Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java HashMap Size

Reply
Thread Tools

Java HashMap Size

 
 
hrpreet
Guest
Posts: n/a
 
      11-24-2005
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.

 
Reply With Quote
 
 
 
 
NullBock
Guest
Posts: n/a
 
      11-24-2005
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.

 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      11-24-2005
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My jython program is not able to show the contents of HashMap of a java program Mahendra Chaurasia Python 0 06-09-2011 09:52 AM
reuse HashMap$Entry (or HashMap in total) to avoid millions of allocations Vince Darley Java 4 03-02-2010 07:48 AM
How is entrySet getting initialized in HashMap.java ? ankur Java 2 02-23-2009 12:06 AM
java.util.Properties extending from HashMap<Object, Object> insteadof HashMap<String, String> Rakesh Java 10 04-08-2008 04:22 AM
New to Java-Question About HashMap JR Java 4 10-10-2004 03:24 PM



Advertisments