为什么是锯齿形图形?

当我使用 NetBeans 运行下面提到的代码时,分配的堆大小图类似于锯齿形状.我附上了 JVisualVM 的屏幕截图,它以锯齿形显示了堆分配图.该程序是一个打印Hello, World!"的简单无限循环.进入控制台.

When I run the below mentioned code using NetBeans, the allocated heap size graph resembles a sawtooth shape. I am attaching the screen capture from JVisualVM which shows the heap allocation graph in with a sawtooth shape. The program is a simple infinite loop printing "Hello, World!" into the console.

public class HelloWorld {
    public static void main(String a[]){
        while(true) {
            System.out.println("Hello, World!");
        }
    }
}

任何人都可以解释使用堆图形状背后的原因吗?

Can anyone explain the reason behind the shape of the graph of used heap?

PS:即使我在不​​使用 NetBeans 的情况下运行它也会发生这种情况,因此它很可能与 NetBeans 无关...

PS: This happens even if I run it without using NetBeans, so it is most likely not related to NetBeans...

推荐答案

堆使用中的锯齿模式可以解释为在调用System.out.println<的过程中创建了几个局部变量/code> 调用.最值得注意的是,在 Oracle/Sun JRE 中,在年轻代中创建了几个 HeapCharBuffer 实例,如下面使用 VisualVM 的内存分析器获得的快照所示:

The sawtooth pattern in the heap usage can be explained by the fact that several local variables are created during the invocation of the System.out.println invocation. Most notably in the Oracle/Sun JRE, several HeapCharBuffer instances are created in the young generation, as noted in the following snapshot obtained using the memory profiler of VisualVM:

有趣的是堆上存在的活动对象的数量.锯齿模式是由伊甸园空间填满时发生的年轻代垃圾收集周期产生的;由于程序中没有执行繁重的计算活动,JVM 能够执行多次循环迭代,从而导致伊甸园空间(4MB 大小)被填满.随后的年轻代收集周期会清除大部分垃圾;它几乎总是整个伊甸园空间,除非对象仍在使用中,如以下从 VisualVM 获得的 gc 跟踪所示:

The interesting bit is in the number of live objects that are present on the heap. The sawtooth pattern results from the young-gen garbage collection cycle that occurs when the eden space fills up; since there is no heavy computational activity performed in the program, the JVM is able to execute several iterations of the loop, resulting in the eden space (of 4MB is in size) filling up. The succeeding young-gen collection cycle then clears out most of the garbage; it is almost always the whole of the eden space, unless the objects are still in use, as indicated by the following gc trace obtained from VisualVM:

因此,锯齿模式的行为可以通过一系列快速连续的对象分配来解释,这些分配填满了伊甸园空间,触发了年轻一代垃圾回收周期;这个过程循环重复,没有延迟,因为底层 JVM 进程没有被另一个进程抢占,并且 JVM 中负责对象分配的主线程也没有被另一个线程抢占.

The behavior of the sawtooth pattern can thus be explained by a series of object allocations in rapid succession that fill up the eden space, triggering a young gen garbage collection cycle; this process repeats cyclically with no delays as the underlying JVM process is not preempted by another process, and the main thread within the JVM that is responsible for the object allocations is also not preempted by another thread.

相关文章