Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell who calls System.gc()?

In a running system, we see lots of "Full GC (System)" which indicates someone triggers System.gc().

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

I did search all the available source but found nothing suspicious so it must be somewhere, probably another app that's running in the same container or the container itself.

like image 331
Aaron Digulla Avatar asked Jul 29 '11 08:07

Aaron Digulla


People also ask

Who is calling System gc?

gc() method runs the garbage collector. Calling this suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.

Is it OK to call system gc?

It's almost always a bad idea to call it because the automatic memory management usually knows better than you when to gc. It will do so when its internal pool of free memory is low, or if the OS requests some memory be handed back.

Which method is executed when we call system gc () in Java?

gc() call is when you want to force finalization, i.e. the call to finalize method.

Can System gc () start the Java garbage collection process?

Developers can call System. gc() anywhere in their code to instruct the JVM to prioritize garbage collection. When a developer calls this method -- and there isn't an extreme load on the JVM -- a Java GC cycle will happen within seconds.


2 Answers

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

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

However a high number of Full GC is more likely to be due to insufficient survivor space or a full tenured space.

Can you try running

jstat -gccause {pid} 5s 
like image 134
Peter Lawrey Avatar answered Sep 28 '22 03:09

Peter Lawrey


Some library which you use might call explicit gc. You can disable it with -XX:-DisableExplicitGC and take a look if it stops Full GC in the logs

like image 37
denis.solonenko Avatar answered Sep 28 '22 02:09

denis.solonenko