Asghar
Asghar

Reputation: 2346

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

Upvotes: 0

Views: 2370

Answers (2)

Ramesh PVK
Ramesh PVK

Reputation: 15446

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

java -Xmx1024m Main

Upvotes: 2

jberg
jberg

Reputation: 4818

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

Upvotes: 4

Related Questions