How much memory does the hash table use?

Will the / hashmap hash table use more memory if it consists only of references to objects and int?

As for the school project, we had to map the database to objects (which is what is now done with orm / hibernate), but strives to find a good way not to store id in objects, in order to save them again, we thought that all objects that we created in hashmap / hashtable so that we can easily get the identifier. My question is whether it will cost me performance, using this, in my opinion, a more elegant way to solve this problem.

+3
source share
3 answers

Would the / hashmap hash table use a lot if it consists only of a link object and an int?

"a lot" depends on how many objects you have. For a few hundred or several thousand you will not notice.

But typically, the default Java collections are really incredibly inefficient when you work with primitives (due to constant boxing / unpacking from a "primitive to a shell", for example, "int to Integer"), both from views and memory points (the two are connected, but not identical).

If you have many records, such as hundreds of thousands or millions, I suggest using, for example, Trove collections.

In your case, you should use this:

TIntObjectHashMap<SomeJavaClass>

or that:

TObjectIntHashMap<SomeJavaClass>

In any case, it will surround default Java collections and cpu-wise by default (and this will cause a path smaller than GC, etc.).

(un) / int/Integer, , ..

Java HashMap<Integer,Integer> Trove TIntIntHashMap berzerk;)

+3

Map.Entry . , int, (, Integer), . Map.Entrys .

, Java.

+1

It is impossible to answer this without any numbers. How many objects do you want to store? Do not forget that you already store objects, so the link to the key / object should be quite small.

The only reasonable thing is to try this and see if it works for you. Do not forget that the JVM will have the maximum memory allocation by default, and you can increase it (if necessary) through-Xmx

0
source

Source: https://habr.com/ru/post/1744241/


All Articles