我做了这个例子,通过 -Xmx64m
我知道这个程序不会泄漏内存(或不泄漏?)。但是我不知道垃圾收集器是怎么做的,因为每次我创建一个新的双数组时。
该程序是否可能存在内存泄漏?因为每次我都会创建一个新的双打数组?
Garbege 收集器在哪里收集新的替身?
public class TestLeakMemory {
private static float total;
public static void main(String[] args) throws InterruptedException {
long currtime = System.currentTimeMillis();
long timeElapsed = 0;
long lastTime = 0;
long count = 0;
while (timeElapsed < 6000000) {
//Thread.sleep(100);
count++;
double arr1[] = new double[200000];
double arr2[] = new double[200000];
double arr3[] = new double[200000];
float doSomething = doSomething(arr1, arr2, arr3);
total += doSomething;
timeElapsed = System.currentTimeMillis() - currtime;
if (System.currentTimeMillis() - lastTime > 3000) {
System.out.println("totalMemory: " + Runtime.getRuntime().totalMemory() + ", freeMemory: " + Runtime.getRuntime().freeMemory() + ", count: " + count);
lastTime = System.currentTimeMillis();
}
}
System.out.println(total);
}
private static float doSomething(double[] arr1, double[] arr2, double[] arr3) {
float total = 0;
for (double d : arr1) {
total += d;
}
for (double d : arr2) {
total += d;
}
for (double d : arr3) {
total += d;
}
return total;
}
}
狐的传说
相关分类