JMX 垃圾收集和 System.gc() 之间的区别?
Difference between JMX Garbage Collection and a System.gc()?
在 VisualVM 中研究我的应用程序的行为时,我遇到了这个问题并感到困惑,我认为执行垃圾收集的 JMX 调用与调用 System.gc()
具有相同的功能,但是在所有环境中我已经试过了,JMX 调用总是导致比调用 System.gc()
更小的堆使用,功能上有什么区别?
您可以看到最后一次下降 - 我手动单击了“执行 GC”按钮,我的使用率比常规系统收集下降了一点。想想为什么会这样?
我已经在多个环境中尝试过这个,将集合留给系统并手动调用System.gc()
,每次JMX调用都会清除更多。
正如您在发布的图像中看到的,系统垃圾收集是 运行,JMX 调用只是清除 more,问题是两者之间有什么区别这两个电话?
这一定是偶然的(因为它在程序执行的不同点被调用或一些类似的因素)-Sun 的 MemoryMXBean
、sun.management.MemoryImpl
:
的实现
public void gc() {
Runtime.getRuntime().gc();
}
System.gc()
:
public static void gc() {
Runtime.getRuntime().gc();
}
所以这两种建议垃圾收集的方法在功能上确实没有区别,唯一的区别在于何时以及从哪个线程调用它。
大体上是一样的,MemoryMXBean.gc()只是调用了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()
在 VisualVM 中研究我的应用程序的行为时,我遇到了这个问题并感到困惑,我认为执行垃圾收集的 JMX 调用与调用 System.gc()
具有相同的功能,但是在所有环境中我已经试过了,JMX 调用总是导致比调用 System.gc()
更小的堆使用,功能上有什么区别?
您可以看到最后一次下降 - 我手动单击了“执行 GC”按钮,我的使用率比常规系统收集下降了一点。想想为什么会这样?
我已经在多个环境中尝试过这个,将集合留给系统并手动调用System.gc()
,每次JMX调用都会清除更多。
正如您在发布的图像中看到的,系统垃圾收集是 运行,JMX 调用只是清除 more,问题是两者之间有什么区别这两个电话?
这一定是偶然的(因为它在程序执行的不同点被调用或一些类似的因素)-Sun 的 MemoryMXBean
、sun.management.MemoryImpl
:
public void gc() {
Runtime.getRuntime().gc();
}
System.gc()
:
public static void gc() {
Runtime.getRuntime().gc();
}
所以这两种建议垃圾收集的方法在功能上确实没有区别,唯一的区别在于何时以及从哪个线程调用它。
大体上是一样的,MemoryMXBean.gc()只是调用了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()