Java垃圾收集器无法正常工作

时间:2018-11-13 05:32:22

标签: java garbage-collection heap

我的问题很简单..如果在任何计算机上运行以下程序,堆将很快变满。那么为什么GC的速度不足以释放内存。 当堆满了 “新的未使用对象”。为什么他们会坚持堆!

public class TestGc {

public String name;

public TestGc next;

public TestGc(String name) {
    this.name = name;
}

public static TestGc create(String nm, int len) {
    TestGc[] tmp = new TestGc[len];
    TestGc prev = null;
    for (int i = 0; i < len; i++) {
        TestGc cur = new TestGc(nm + i);
        tmp[i] = cur;
        if (prev != null)
            prev.next = cur;

        prev = cur;
    }
    return tmp[0];
}

public static void main(String[] args) {
    if (args.length != 1) {
        System.err.println("Usage: TestGc <number of roots");
        return;
    }

    int num = Integer.valueOf(args[0]);

    long start = System.currentTimeMillis();

    TestGc[] arr = new TestGc[num];
    for (int i = 0, j = 0; i < 1000; i++, j++) {
        if (j == num)
            j = 0;
        String nm = "root_" + i + "_";
        System.out.println("******** " + nm);
        arr[j] = create(nm, 1000000);
    }
    double duration = (System.currentTimeMillis() - start) / 1000.0;
    System.out.println("******** duration " + duration + " sec.");
}

}

java -Xmx23g -Xms23g -server -XX:+UseG1GC -XX:+PrintGCDateStamps -XX:+PrintGCDetails TestGc 150

示例从此处复制。 https://bugs.openjdk.java.net/browse/JDK-8065402

1 个答案:

答案 0 :(得分:0)

发生这种情况是因为除"i18next": "^12.0.0", "i18next-browser-languagedetector": "^2.0.0", "i18next-express-middleware": "^1.4.1", "i18next-node-fs-backend": "^2.1.0", "i18next-xhr-backend": "^1.4.3", "prop-types": "^15.6.0", "react": "^16.6.1", "react-dom": "^16.3.1", "react-i18next": "^8.3.5", "webpack": "^4.24.0", 方法中tmp引用的数组以外的所有对象都是可访问的,因此它们不应由GC处理。