垃圾收集不会收集JDK 1.8中的对象数组

时间:2019-05-29 09:54:34

标签: java garbage-collection

垃圾收集不适用于收集数组

我将数组引用设置为null并立即调用System.gc()运行垃圾回收,但是ram中的空间对于java.exe仍然有效。

public class Main {

    public static void main(String[] args) {
        Sort sort = new Sort();
        sort.largeDataSort();
        System.out.println("GC in Main start");
        sort = null;
        long startTime = System.currentTimeMillis();
        System.gc();
        System.out.println("GC in Main end, total time: " + (System.currentTimeMillis() - startTime));

        while (true){
        }
    }
}
public class Sort {
    public static void largeDataSort(){
        System.out.println("Start the sorting algorithm");
        Integer[] arr= new Integer[100000000];
        Random random = new Random();
        for (int i = 0; i < 100000000; i++){

            int v = random.nextInt();
            arr[i]=v;
        }

        Arrays.fill(arr, null);
        arr = null;
        long startTime = System.currentTimeMillis();
        System.gc();
        System.out.println("GC in Sort class end, total time: " + (System.currentTimeMillis() - startTime));
    }
}

实际结果:内存使用量2625.MB

预期结果:远低于此结果

0 个答案:

没有答案