package org.example.menory;
import java.util.ArrayList;
class Student {
int age = 1;
String name = "xx";
}
public class TestMemory {
public static void main(String[] args) throws Exception {
ArrayList arrayList = new ArrayList<>();
while (true) {
for (int i = 0; i < 100000; i++) {
arrayList.add(new Student());
}
int curMemory = (int) (Runtime.getRuntime().totalMemory() / 1000 / 1000);
int totalMemory = (int) (Runtime.getRuntime().maxMemory() / 1000 / 1000);
System.out.println(curMemory + "/" + totalMemory);
Thread.sleep(80);
if (arrayList.size() > 1000000) {
arrayList.clear();
System.out.println("clear!!!");
}
}
}
}
52/259
52/259
52/259
52/259
52/259
52/259
52/259
52/259
52/259
52/259
52/259
clear!!!
可以看出来,java还是比较强的,如果没有内存泄漏,一直不释放这种情况(比如一直往ArrayList中添加),那么还是保持内存十分的稳定的。