Why do I get an OutOfMemoryError when inserting 50,000 objects into HashMap?

前端 未结 10 1933
猫巷女王i
猫巷女王i 2021-02-01 08:12

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

相关标签:
10条回答
  • 2021-02-01 08:54

    Also might want to take a look at this:

    http://java.sun.com/docs/hotspot/gc/

    0 讨论(0)
  • 2021-02-01 08:54

    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.

    0 讨论(0)
  • 2021-02-01 08:57

    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.

    0 讨论(0)
  • 2021-02-01 09:00

    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.

    0 讨论(0)
提交回复
热议问题