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

前端 未结 10 1953
猫巷女王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 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.

提交回复
热议问题