Best way to store 1000000 phone numbers in memory [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
What would be the best way to store 1000000 phone numbers in memory with the smallest memory footprint.
I was thinking of just using an array but im sure there has to be an better way

The size of memory scales not significantly with the way you store the collection (!) of numbers, but more with how you actually store one phone number (as a string, or as an integer).
If you really want to reduce memory, try to store each phone number using an long.
For instance if you store phone numbers in an ArrayList you will get a maximum overhead of say 30%, which is not that much. If you however store each phone number as a string, you will get an overhead of let's say 900% compared to storing data using integers.

The array has the smallest memory footprint.

make an algorithm for phone number. pretty complex. if you can do it though, that would save a lot of memory.

Related

Disadvantage using Map in terms of Memory Utilization in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Are there any disadvantages (in terms of memory) for using Maps in Java?
Because as per my knowledge if you are using HashMaps or any other collections Classes .
For eg.
Map<String , String> map = new HashMap<String, String>();
map.put("id",1);
map.put("name","test123");
So I used 2 bites for each one of those let's assume.
And according to me Map or any other collection hold 100 bites so remaining 98 bites are wasted.
So, for that scenario, can I use anything else?
For a description of initial capacity and load factor, see What is the significance of load factor in HashMap?
If you use arrays, you will probably use less memory than when you use a map. But in most cases, the ease of use and readabilty is far more important than memory usage.
See also Hash Map Memory Overhead for a description of HashMaps memory usage.
First of all, yes, creating a map has some memory overhead for very small amounts of data. It creates arrays with Entry wrapper classes for the given load capacity/load factor. So, you might be wasting a few bytes, but in the age of gigabyte-sized memory, that would only become an issue when you would be creating millions or even billions of maps, depending on how much memory you actually give your application and what other things it has to manage.
If I know that a collecting will remain really small and I'm not using the keys anyway, I sometimes just use a list instead, because checking 2 or 4 elements is quite fast anyway. In the end, do not even bother to worry about such minor things until they are taking up a major slice of available memory.

How to fit large table in memory? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a Java Map<String, List<String>>.
Is there a way to improve this to make it use less memory without having too much impact on performance?
Three ideas:
An encoded byte array could provide a less memory-intensive representation than a string, especially if the string data actually uses an 8 bit (or less) character set.
A list of strings could be represented as a single string with a distinguished string separator character between the list components.
String data is often compressible.
Depending on the nature of your data, these could easily give a 2 fold reduction in space for the lists.
The downside is that you may need to fully or partially reconstruct the original List<String> objects, which would be a performance hit.
You should also consider using a non-memory resident representation; e.g. a conventional database, a NOSQL database or an "object cache" framework. JVMs with really large heaps tend to lead to performance problems if you need to do a "full" garbage collection, or if there is competition for physical memory with other applications.
One would really need to know a lot more on your specific application to definitely recommend a specific solution, but as a wild guess, if it is a really, really large table (e.g hundreds of thousands or millions of records), I would suggest you consider using a database to store data and access via one of data layer access abstractions, such as DataSet.
Databases are already optimized to efficiently store, search and access data over an amortized data and time range, so without further info on your application, I would go with this option.

Complete word-database for Java-App to check if a word is actually a legit word, is SQL appropriate in this case? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am going to write a game in which I have often have to check if a string of letters is actually a word or not. My question is about how to do this the fastest with the least computation-able power as possible (for instance an old smart-phone). With if possible not much start-up time to make it a quick and responsive app.
I once did this look-up by first reading in a word-file with almost all words into an appropriate sized hash-map of around 650,000 words*. (* might be more, I am not sure if this is the exhausted list yet).
Would a SQL database be appropriate here? I am thinking of buying a book about it so I can learn and implement one. Also I have no idea how you could create a hash-map, save it for later and then load one. Is that too much of a hacker solution or is that technique used more often? So would it make sense for me to learn SQL or do it with saving a hashmap and then later restoring it.
A database SQL could be appropriate if you plan to query it every time you need to check a word, but this is not the fastest solution; querying every single word slows down the response time but it should use less memory if the words number is high (you must measure the memory consumed by the db vs the memory consumed by the map). Checking if a word is inside a map is not so computationally expensive, it must calculate the hash and iterate over the array of items with the same hash.
Personally I would choose a map if the memory requirements of keeping all the words in memory can be satisfied. You can store the dictionary as plain text file (one line -> one word) and read it in a background thread when the application starts.
If memory is an issue, this seems like a good use for a B-Tree. This allows for O(log n) search time while searching a large amount of records with minimal memory usage. For this sort of application it sounds like loading the entire thing into memory is not going to be a good idea.

Fixed length array vs fields [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
What is faster?
I want to write an API for processing and calculating with vectors and matrices.
A "Matrix4f" needs 4*4 float values.
Should i write this as 16 fields or a two-dimensional array?
But if i use fields, inheritance is impossible.
This is more a question of maintainability than speed. The speed difference between your two alternatives will almost certainly not be noticeable. The array approach, however, makes more sense in terms of what you are trying to model, and it's simply easier to deal with (say, for instance, you want to create a 5x5 matrix instead, then your array code will be easily reusable whereas your code with 16 fields would require drastic modifications). In short, don't worry about speed when making this decision, worry instead about what makes more sense and what will be easier to manage down the line; then the choice should be clear.
There is no complexity in accessing an array nor in accessing a variable, O(1) so to say.
This is not what you should consider in speed, but your actual algorithms and functions.

Algorithm for finding trends in data? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm looking for an algorithm that is able to find trends in large amounts of data. For instance, if one is given time t and a variable x, (t,x), and given input such as {(1,1), (2,4), (3,9), (4,16)}, it should be able to figure out that the value of x for t=5 is 25. How is this normally implemented? Do most algorithms compute lines of best fit that are linear, quadratic, exponential, etc. and then chooses the line of best fit with the lowest standard deviation? Are there other techniques for finding trends in data? Also, what happens when you increase the number of variables to analyze large vectors?
This is a really complex question, try to start from: http://en.wikipedia.org/wiki/Interpolation
There is no simple answer for a complex problem: http://en.wikipedia.org/wiki/Regression_analysis
A Neural network might be a good candidate. Especially if you want to learn it something nonlinear.

Categories