0

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 error "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?

trincot
  • 317,000
  • 35
  • 244
  • 286
Asghar
  • 2,336
  • 8
  • 46
  • 79
  • 2
    increase your heap by specifying the -Xmx parameter when launching the virtual machine. – clamp Jun 02 '11 at 09:42
  • 1
    possible duplicate of [How to deal with "java.lang.OutOfMemoryError: Java heap space" error (64MB heap size)](http://stackoverflow.com/questions/37335/how-to-deal-with-java-lang-outofmemoryerror-java-heap-space-error-64mb-heap-s) – x4u Jun 02 '11 at 09:48

2 Answers2

4

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jberg
  • 4,758
  • 2
  • 20
  • 15
2

Increase the Java heap size with the -Xmx Java system property. For example, give it as

java -Xmx1024m Main
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50