Java heap space error while creating ArrayList of large number of objects?
I have created an ArrayList (Java) of my custom class objects with a size around 3000. But when I run my code it gets the err开发者_如何学Goor "Heap space error".
I want to keep thousands of objects in an ArrayList at runtime without getting out of heap space.
How can the heap space error be avoided?
It seems like you need to pass your program more memory.
Try running it like this:
java -Xmx256M MyApp
-Xmx
sets the maximum heap size for Java. Putting M afterwards means megabytes, and G afterwards means gigabytes. So you can always do this if you have a bunch of memory:
java -Xmx1g MyApp
Increase the Java heap size with the -Xmx
Java system property. For example, give it as
java -Xmx1024m Main
精彩评论