简体   繁体   中英

Difference between JMX Garbage Collection and a System.gc()?

When studying the behavior of my application in VisualVM, I encountered this and was puzzled, I figured that the JMX call to Perform a Garbage Collection would have the same functionality as calling System.gc() , however in all environments that I have tried it on, the JMX call always results in a smaller heap usage then the call to System.gc() , what functionally is the difference?

在此输入图像描述

You can see on the final drop - I manually clicked the Perform GC button, my usage dropped a bit lower then it had been with the regular system collections. Thoughts on why this might be?

I have tried this in multiple environments, leaving the collections up to the system and manually invoking System.gc() , and every time the JMX call will clear a lot more.

As you can see in the posted image, the system garbage collection IS running, the JMX call just clears more , the question is what is the difference between these two calls?

This must be circumstantial (due to it being called at a different point in program's execution or some similar factor) - the Sun's implementation of MemoryMXBean , sun.management.MemoryImpl :

public void gc() {
    Runtime.getRuntime().gc();
}

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/sun/management/MemoryImpl.java#MemoryImpl.gc%28%29

System.gc() :

public static void gc() {
    Runtime.getRuntime().gc();
}

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/lang/System.java#System.gc%28%29

So there's really no functional difference between the two ways to suggest garbage collection, and the only difference is in when, and from what thread, it is called.

In general it's the same, the MemoryMXBean.gc() just call System.gc().

MemoryMXBean.gc()

void gc() Runs the garbage collector. The call gc() is effectively equivalent to the call: System.gc() See Also: System.gc()

http://docs.oracle.com/javase/7/docs/api/java/lang/management/MemoryMXBean.html

System.gc()

public static void gc() Runs the garbage collector. 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.

The call System.gc() is effectively equivalent to the call:

Runtime.getRuntime().gc() See Also: Runtime.gc()

http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#gc()

Runtime.gc()

public void gc() Runs the garbage collector. Calling this 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 virtual machine has made its best effort to recycle all discarded objects. The name gc stands for "garbage collector". The virtual machine performs this recycling process automatically as needed, in a separate thread, even if the gc method is not invoked explicitly.

The method System.gc() is the conventional and convenient means of invoking this method.

http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#gc()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM