java.lang.OutOfMemoryError: Java heap space

Viewed 328082

I am getting the following error on execution of a multi-threading program

java.lang.OutOfMemoryError: Java heap space

The above error occured in one of the threads.

  1. Upto my knowledge, Heap space is occupied by instance variables only. If this is correct, then why this error occurred after running fine for sometime as space for instance variables are alloted at the time of object creation.

  2. Is there any way to increase the heap space?

  3. What changes should I made to my program so that It will grab less heap space?

12 Answers

To avoid that exception, if you are using JUnit and Spring try adding this in every test class:

@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)

I have tried all Solutions but nothing worked from above solutions

Solution: In My case I was using 4GB RAM and due to that RAM usage comes out 98% so the required amount if Memory wasn't available. Please do look for this also.If such issue comes upgrade RAM and it will work fine.

Hope this will save someone Time

Related