I am trying to insert about 50,000 objects (and therefore 50,000 keys) into a java.util.HashMap
. However, I keep getting an OutOfMemo
Also might want to take a look at this:
http://java.sun.com/docs/hotspot/gc/
Random thought: The hash buckets associated with HashMap are not particularly memory efficient. You may want to try out TreeMap as an alternative and see if it still provide sufficient performance.
By default, the JVM uses a limited heap space. The limit is JVM implementation-dependent, and it's not clear what JVM you are using. On OS's other than Windows, a 32-bit Sun JVM on a machine with 2 Gb or more will use a default maximum heap size of 1/4 of the physical memory, or 512 Mb in your case. However, the default for a "client" mode JVM is only 64 Mb maximum heap size, which may be what you've run into. Other vendor's JVM's may select different defaults.
Of course, you can specify the heap limit explicitly with the -Xmx<NN>m
option to java
, where <NN>
is the number of megabytes for the heap.
As a rough guess, your hash table should only be using about 16 Mb, so there must be some other large objects on the heap. If you could use a Comparable
key in a TreeMap
, that would save some memory.
See "Ergonomics in the 5.0 JVM" for more details.
You probably need to set the flag -Xmx512m or some larger number when starting java. I think 64mb is the default.
Edited to add: After you figure out how much memory your objects are actually using with a profiler, you may want to look into weak references or soft references to make sure you're not accidentally holding some of your memory hostage from the garbage collector when you're no longer using them.