简体   繁体   English

从YourKit调用System.gc()导致分配的内存增加

[英]Invoking System.gc() from YourKit causes allocated memory to increase

I'm about to start using a 3rd party closed-source library to load a bunch of data and wanted to check how fast it is and how much memory it requires to load one 'set' of data. 我将要开始使用第三方封闭源库来加载一堆数据,并想检查它的速度以及加载一组“数据”所需的内存量。 I wrote this simple harness to invoke a data load to time it and used YourKit to have a quick look at memory usage and delve in to the CPU time. 我编写了这个简单的工具来调用数据加载以计时,并使用YourKit快速查看内存使用情况并深入研究CPU时间。

I'm running it on Windows 7, using Eclipse on JDK8 with no VM args. 我在Windows 7上运行它,并且在没有VM参数的情况下在JDK8上使用Eclipse。

public static void main(String[] args){
    long start = System.currentTimeMillis();

    // There are a few more calls involved, but not much
    BlackBoxDataProvider bd = new BlackBoxDataProvider("c:\\thedata");
    BlackBoxData = bd.loadTheData();
    System.out.println(System.currentTimeMillis() - start + "ms");

    // Keep the application alive so I can have a quick look at memory usage
    while(true) {
        Thread.sleep(1000);
    }
}

Here's the YourKit snapshot of memory after the load is complete: 这是加载完成后的YourKit内存快照:

在此处输入图片说明

I then used YourKit to "Force" Garbage Collection and this happened: 然后,我使用YourKit来“强制”收集垃圾,这发生了:

在此处输入图片说明

Obviously it's not a real life scenario because I'm stuck inside the main method, on the main thread, so some of my references won't be cleaned up, but I can't figure out why the memory allocation would keep increasing. 显然,这不是现实生活中的情况,因为我被卡在主方法的主线程中,因此我的一些引用不会被清理,但是我无法弄清楚为什么内存分配会不断增加。

Every time I click 'Force System GC', the allocation increases. 每次单击“强制系统GC”,分配都会增加。 I got up to 11.9GB before it stopped increasing. 在停止增加之前,我已达到11.9GB。

在此处输入图片说明

Why is this happening? 为什么会这样呢?

The System.gc() will return when all objects have been scanned once. 扫描完所有对象一次后,System.gc()将返回。 If objects implementing the finalize() method are added to a queue, to be cleaned up later. 如果将实现finalize()方法的对象添加到队列中,以便稍后进行清理。 This means those objects cannot be cleaned up yet (not the queue nodes which hold them) ie the act of triggering a GC can increase memory consumption temporarily. 这意味着还不能清除那些对象(不是保存它们的队列节点),即触发GC的行为可能会暂时增加内存消耗。 This is what might be happening in your case. 这就是您的情况。

In short, not all objects can be cleaned up in one cycle. 简而言之,并非所有对象都可以在一个周期内清理干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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