问题
When I run jcmd PID help GC.heap_dump
, the help clearly says that full GC will be called, unless -all
flag is specified:
GC.heap_dump ... Impact: High: Depends on Java heap size and content. Request a full GC unless the '-all' option is specified. <...>
-all : [optional] Inspect all objects, including unreachable objects (BOOLEAN, false)
If I run jcmd PID help GC.class_histogram
, the help doesn't say anything about forcing a full GC, however "Impact" is still said to be "high", and the option still has an -all
flag, which behaves exactly as for GC.heap_dump
:
GC.class_histogram ... Impact: High: Depends on Java heap size and content.
-all : [optional] Inspect all objects, including unreachable objects (BOOLEAN, false)
I tried to run this command on couple of environments, and full GC was not called. However, since it "Depends on Java heap size and content" I cannot be sure.
So can jcmd PID GC.class_histogram
call a full GC in some circumstances? If yes, what are they?
回答1:
jcmd PID GC.class_histogram
will cause Full GC by default.
If the target JVM is launched with -XX:+PrintGC
, you will see a log message like
// JDK 8:
[Full GC (Heap Inspection Initiated GC) 1397K->331K(126720K), 0.0023298 secs]
// JDK 9:
[15.503s][info ][gc] GC(0) Pause Full (Heap Inspection Initiated GC) 2M->0M(8M) 8.107ms
However, with -all
option there will be no Full GC for GC.class_histogram
, exactly like for GC.heap_dump
. Find the proof in HotSpot sources:
void ClassHistogramDCmd::execute(DCmdSource source, TRAPS) {
VM_GC_HeapInspection heapop(output(),
!_all.value() /* request full gc if false */);
VMThread::execute(&heapop);
}
回答2:
You could try to run jcmd PID GC.run
before jcmd PID GC.class_histogram
jcmd PID help GC.run
PID:
GC.run
Call java.lang.System.gc().
Impact: Medium: Depends on Java heap size and content.
Syntax: GC.run
But I'm not sure that there is any guarantee that JVM will actually perform GC upon that request. Javadoc for System.gc()
says this:
When control returns from the method call, the Java Virtual
Machine has made a best effort to reclaim space from all discarded objects
来源:https://stackoverflow.com/questions/48446767/will-jcmd-pid-gc-class-histogram-call-a-full-gc-before-collecting-data