[英]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? 当我在VisualVM中研究我的应用程序的行为时,我遇到了这个并且感到困惑,我认为执行垃圾收集的JMX调用与调用
System.gc()
具有相同的功能,但是在我尝试过的所有环境中在,JMX调用总是导致较小的堆使用量,然后调用System.gc()
,功能上有什么区别?
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. 你可以在最后一滴上看到 - 我手动点击了执行GC按钮,我的使用率比常规系统集合略低。 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. 我已经在多个环境中尝试过这个,将集合留给系统并手动调用
System.gc()
,每次JMX调用都会清除更多。
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? 正如您在发布的图像中看到的那样,系统垃圾收集正在运行,JMX调用只是清除了更多 ,问题是这两个调用之间有什么区别?
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
: 这必须是间接的(由于它在程序执行中的不同点或类似因素被调用) - Sun的
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 http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/sun/management/MemoryImpl.java#MemoryImpl.gc%28%29
System.gc()
: 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 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()只调用System.gc()。
MemoryMXBean.gc() MemoryMXBean.gc()
void gc() Runs the garbage collector.
void gc()运行垃圾收集器。 The call gc() is effectively equivalent to the call: System.gc() See Also: System.gc()
调用gc()实际上等同于调用:System.gc()参见:System.gc()
http://docs.oracle.com/javase/7/docs/api/java/lang/management/MemoryMXBean.html http://docs.oracle.com/javase/7/docs/api/java/lang/management/MemoryMXBean.html
System.gc() System.gc()的
public static void gc() Runs the garbage collector.
public static void gc()运行垃圾收集器。 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.
调用gc方法表明Java虚拟机花费了大量精力来回收未使用的对象,以使其当前占用的内存可用于快速重用。 When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
当控制从方法调用返回时,Java虚拟机已尽最大努力从所有丢弃的对象中回收空间。
The call System.gc() is effectively equivalent to the call:
调用System.gc()实际上等同于调用:
Runtime.getRuntime().gc() See Also: Runtime.gc()
Runtime.getRuntime()。gc()参见:Runtime.gc()
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#gc() http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#gc()
Runtime.gc() Runtime.gc()
public void gc() Runs the garbage collector.
public void gc()运行垃圾收集器。 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.
调用此方法表明Java虚拟机花费了大量精力来回收未使用的对象,以使其当前占用的内存可用于快速重用。 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".
名称gc代表“垃圾收集器”。 The virtual machine performs this recycling process automatically as needed, in a separate thread, even if the gc method is not invoked explicitly.
即使未明确调用gc方法,虚拟机也会根据需要在单独的线程中自动执行此回收过程。
The method System.gc() is the conventional and convenient means of invoking this method.
System.gc()方法是调用此方法的传统方便方法。
http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#gc() http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#gc()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.