Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding PermGen out of memory and GC overhead limit exceeded

I'm trying to generate classes and load them at run time.
I'm using a ClassLoader object to load the classes. Since I don't want to run out of PermGen memory, from time to time I un-reference the class loader and create a new one to load the new classes to be used. This seems to work fine and I don't get a PermGen out of memory. The problem is that when I do that, after a while I get the following error:

java.lang.OutOfMemoryError: GC overhead limit exceeded 

So my question is, when should I un-reference the class loader to avoid both errors?:
Should I monitor in my code the PermGen usage so that I un-reference the class loader and call System.gc() when the PermGen usage is close to the limit?
Or should I follow a different approach?
Thanks

like image 236
pablo Avatar asked Oct 24 '13 17:10

pablo


People also ask

What does'GC overhead Limit Exceeded'mean?

14 Answers. GC overhead limit exceeded" indicates that the garbage collector is running all the time and Java program is making very slow progress. After a garbage collection, if the Java process is spending more than approximately 98% of its time doing garbage collection and if it is recovering less than 2% of the heap...

How to fix GC overhead Limit Exceeded error in MVN exec?

mvn exec: exec. It should also be noted that in some situations we might encounter a heap space error before encountering the GC Overhead Limit Exceeded error. 4. Solving GC Overhead Limit Exceeded Error. The ideal solution is to find the underlying problem with the application by examining the code for any memory leaks.

What does it mean when garbage collection takes up 98% of memory?

This message means that for some reason the garbage collector is taking an excessive amount of time (by default 98% of all CPU time of the process) and recovers very little memory in each run (by default 2% of the heap). This effectively means that your program stops doing any progress and is busy running only the garbage collection at all time.

What is the JVM GC overhead error?

Simply put, the JVM takes care of freeing up memory when objects are no longer being used; this process is called Garbage Collection ( GC ). The GC Overhead Limit Exceeded error is one from the family of java.lang.OutOfMemoryError and is an indication of a resource (memory) exhaustion.


2 Answers

There is no single correct answer to this.

On the one hand, if unlinking the classloader is solving your permgen leakage problems, then you should continue to do that.

On the other hand, a "GC overhead limit exceeded" error means that your application is spending too much time garbage collection. In most circumstances, this means that your heap is too full. But that can mean one of two things:

  • The heap is too small for your application's requirements.

  • Your application has a memory leak.

You could assume that the problem is the former one and just increase the heap size. But if the real problem is the latter one, then increasing the heap size is just postponing the inevitable ... and the correct thing to do would be to find and fix the memory leak.


Don't call System.gc(). It won't help.

like image 156
Stephen C Avatar answered Nov 11 '22 16:11

Stephen C


Are you loading the same class multiple times? Because you should cache the loaded class.

If not, how many classes are you loading? If they are plenty you may have to fix a limit of loaded classes (this number can be either based on heap size or a number based on how much memory does it take to have a loaded class) and discard the least used when loading the next one.

like image 29
Bruno Costa Avatar answered Nov 11 '22 16:11

Bruno Costa