Java HashMap Size

Discussion in 'Java' started by hrpreet, Nov 24, 2005.

  1. hrpreet

    hrpreet Guest

    What can be maxium capacity of a HashMap so that performance of code is
    not effected.Is it ok to store lackhs of data in a HashMap.If not then
    which java collection is suitable for storing this large a data.
    hrpreet, Nov 24, 2005
    #1
    1. Advertising

  2. hrpreet

    NullBock Guest

    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.)

    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.

    Walter Gildersleeve
    Freiburg, Germany
    ____________________________________________________
    http://linkfrog.net
    URL Shortening
    Free and easy, small and green.
    NullBock, Nov 24, 2005
    #2
    1. Advertising

  3. 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/
    Thomas Hawtin, Nov 24, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vince Darley
    Replies:
    4
    Views:
    4,420
    emilchacko
    Mar 2, 2010
  2. JR
    Replies:
    4
    Views:
    700
  3. Rakesh
    Replies:
    10
    Views:
    12,174
    Mike Schilling
    Apr 8, 2008
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,565
    Michael Jung
    May 25, 2008
  5. ankur
    Replies:
    2
    Views:
    2,324
    ankur
    Feb 23, 2009
Loading...

Share This Page