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?

2

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

2

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

java -Xmx1024m Main

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.