假设我有以下代码:
public void process() {
byte[] data = new byte[size];
... // code that uses the above data
longProcess(); // a very long running process that does not use the data.
}
假设数据在程序的其他任何地方都没有被引用,JVM 是否足够智能以允许在长进程仍在运行时对数据进行垃圾收集?
如果没有,会添加
data = null;
在漫长的过程允许这种情况发生之前?
最佳答案
这取决于 JVM。我试过的 Oracle JVM 版本(1.6.0_41 和 1.7.0_09)默认情况下不执行此优化。但是,1.7.0_09 会在打开积极优化时执行它。
这是我进行的测试:
public class Main {
public static int g() {
int n = 100000;
int arr[][] = new int[n][];
for (int i = 0; i < n; ++i) {
try {
arr[i] = new int[100000];
} catch (OutOfMemoryError ex) {
return i;
}
}
return -1;
}
public static void f1() {
int arr[] = new int[1000000];
System.out.println(g());
}
public static void f2() {
int arr[] = new int[1000000];
arr = null;
System.out.println(g());
}
public static void main(String[] argv) {
for (int j = 0; j < 2; ++j) {
for (int i = 0; i < 10; ++i) {
f1();
}
System.out.println("-----");
for (int i = 0; i < 10; ++i) {
f2();
}
System.out.println("-----");
}
}
}
使用具有默认设置的 JVM 1.7,f1()
在 3195 次迭代后始终耗尽内存,而 f2()
始终管理 3205 次迭代。
如果使用 Java 1.7.0_09 和 -XX:+AggressiveOpts -XX:CompileThreshold=1
运行代码,图片会发生变化:两个版本都可以进行 3205 次迭代,表明 HotSpot 确实执行了此优化在这种情况下。 Java 1.6.0_41 似乎没有这样做。
在我的测试中,限制数组的范围与设置引用 null
具有相同的效果,如果您觉得应该帮助 JVM 尽快收集数组,这可能是首选.
关于基于堆栈的数组上的 Java 垃圾收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15233471/