Reputation: 647
There is a button "Perform GC" in jconsole, anyone knows what exactly happens if I click that button, it's invoking System.gc()
?
Upvotes: 4
Views: 5982
Reputation: 15719
You can find out by yourself. The code of JConsole is part of OpenJDK. You can also check it out on grepcode.com.
The button calls a gc()
method of an object implementing MemoryMXBean
which is most probably implemented by com.sun.management.MemoryImpl
class. This class contains an implementation of gc()
method which looks like this:
public void gc() {
Runtime.getRuntime().gc();
}
Now, if you consider the implementation of gc()
method in java.lang.System
which looks like this:
public void gc() {
Runtime.getRuntime().gc();
}
the answer to your question is: Technically no, but it does the same thing.
Upvotes: 5
Reputation: 10055
I fear they do pretty much the same under the hood, but I'm not sure I agree with the description that says:
The Memory tab features a “Perform GC” button that you can click to perform garbage collection whenever you want.
When actually System.gc()
is just a suggestion for the VM:
Calling the gc method 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. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
Should they do the same, I would not dare to say you can do it whenever you want.
Upvotes: 0
Reputation: 16066
Pretty much. The GC Button in the MemoryTab in JConsole calls MemoryTab.gc() which in turn calls the target JVM's MemoryMXBean.gc(). The javadoc says this call is equivalent to java.lang.System.gc().
Upvotes: 0