How can I tell who is calling System.gc ()?

On a running system, we see a lot of "Full GC (System)", which indicates that someone is starting System.gc ().

Is there a way to find out where in the code this happens?

I searched for the entire available source, but did not find anything suspicious, so it should be somewhere, maybe another application running in the same container or the container itself.

+7
source share
4 answers

You can change the Runtime class to a log where gc () is called into a file (calls to System.gc () Runtime.gc ())

To do this, edit the copy, compile it and add to your -Xbootclasspath/p:

However, a large amount of Full GC is most likely due to insufficient survivors or full space.

Can you try run

 jstat -gccause {pid} 5s 
+8
source

Some library that you use may explicitly call gc. You can disable it with -XX:-DisableExplicitGC and see if it stops Full GC in the logs

+3
source

In Java, you cannot β€œcall” the garbage collector. If you see the documentation when you call the System.gc () method, vm simply takes the offer to run the garbage collector, but there is no guarantee what is actually called. It is an internal process that will be called automatically, even if you do not call it when it needs space on the stack.

If you see a lot of full GC, this may be a sign that the application is not freeing resources correctly. You have to keep an eye on the bunch to see what the problem is.

+2
source

Using the following script bracket and jvisualvm using the btrace plugin will easily identify the caller of System.gc .

 // import all BTrace annotations import com.sun.btrace.annotations.*; // import statics from BTraceUtils class import static com.sun.btrace.BTraceUtils.*; // @BTrace annotation tells that this is a BTrace program @BTrace public class GCcaller { // @OnMethod annotation tells where to probe. // In this example, we are interested in entry // into the Thread.start() method. @OnMethod( clazz="java.lang.System", method="gc" ) public static void func() { // println is defined in BTraceUtils // you can only call the static methods of BTraceUtils println("about to start a System.gc!"); jstack(); } } 
+2
source

All Articles