2

Getting below error in eclipse.

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space.

can someone know that how to fix this issue?

Regards,
Mahendra Athneria

3

3 Answers 3

4

Use less memory.

It's hard to say exactly what the problem is without knowing what kind of application you're running. So this answer is just going to contain general advice for dealing with this problem.

Use the jvisualvm application which comes with the JDK. It will allow you to connect to a running process and see a breakdown of how the memory is being used.

Look in the results for large numbers of objects that should not be there. If you see that there are 20,000 instances of one class, do the math and see if there should actually be that many. Look for the object types that have way more instances than there should actually be. Then you have to figure out why you have so many.

These extra objects are likely being created and not properly dereferenced. You may be putting them in a data structure somewhere and not removing them. You may have threads that are still running which reference this data which aren't ending like they should. There are all sorts of reasons why you may be continuing to reference data that should be garbage collected.

The key piece of information is knowing which object class is exploding in quantity like this. Once you know that, you can override the constructor and destructor of that class to log how many of them have been created and destroyed, and where. By pouring through this data, you can determine where the extra objects are being made, or where they are not being destroyed. Once you correct this problem, you will stop getting the error.

Of course, it's also entirely possible that the data you are processing really actually needs this amount of memory. You've told us nothing about your project, so it's impossible to judge this accurately. If this is the case, then you need to figure out how to partition the data to load only part of it at a time, process it, free it from memory, and then move onto the next chunk of data.

I've had these problems before and used all of the above techniques to solve them.

1

Is eclipse running of memory or an application you are running from within eclipse?

Assuming that its not a memory leak and you just need more memory you can alter the VM arguments passed to an application running inside eclipse. Run Configurations -> VM Arguments -> -Xmx512m

If the problem is eclipse then you can edit the settings file eclipse.ini and change the -Xmx argument.

However if you do have a memory leak, increasing the maximum heap size will just hide the problem for a little bit longer.

0

Instead of forcing System.gc() as @keatch suggested I think it's better to find out what's the memory leakage. There's a tool jHat (Java Heap Analysis Tool) which can help finding out the cause of the problem.

Here's an explanation how to make a heap dump.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.