Re: Java HashMap Size



NullBock wrote:
Looking at the source, it looks like HashMap needs about 16 + 4 *
(loadfactor) bytes per entry, plus a few bytes.  Each bucket has four
fields, each field using four bytes.  The pointer to the bucket is also
necessary, and since it's hashed, there are more bucket pointers than
actual buckets.  This really isn't very many, and is probably dwarfed
by the memory need for the items being stored.  (IE, a simple string
can use hundreds of bytes.)

It uses a bit more memory than this. Each object has (typically) a pointer to the class information, a hash code and bits for use in synchronisation and for garbage collection. Also on 64-bit systems, the reference size will be doubled.


You could write a hash map with the entries inlined into the main array, but it would tend to make any operation using Map.Entry slow.

The map may be many-to-one and the key may be shared or small, so memory may be an issue in some obscure cases. A more likely (but still unlikely) problem is GC pauses, particular if entries are medium lived.

In any case, the upper limit for a hash table is the JVM memory
allocation, so even gargantuan hash maps shouldn't be a problem.  And
the great thing about hash maps is, even if they are gargantuan (ie,
millions of entries), the access time is still very fast.

Yup, because the table grows, the number of entries necessary to sort through on every access remains very small.


Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
.



Relevant Pages

  • Re: RMS indexed file structure questions
    ... Prolog - The prolog contains important file-wide information and is ... a pointer to the next key descriptor. ... each bucket below it in the structure. ... index compression, index uninitialized, key compression, ...
    (comp.os.vms)
  • Show ARP cache on AIX?
    ... What do I have to do to show the ARP cache on an AIX server? ... bucket: 0 contains: 0 entries ...
    (comp.unix.aix)
  • SGI hash_map
    ... I know that hash maps aren't in the standard. ... (or is there an SGI library newsgroup?) ... I am currently testing the hash_map implementation of SGI. ... into a own bucket and all buckets were used. ...
    (comp.lang.cpp)
  • Runtime Error with MM2 HELP PLEASE!!!
    ... Here's how to get the bucket number: ... Use Movie Maker to generate the crash. ... Look for entries whose "Type" is "error" and have ... >error window pops up saying (in a frame that says Visual ...
    (microsoft.public.windowsxp.moviemaker)
  • Re: If vector contains only different elements
    ... > insert a pointer to the new element in the set at that hash table entry. ... But do the proposed hash_table interfaces expose this ... It seems we want to get the bucket an element hashes to, ...
    (comp.lang.cpp)