public static class TestMemory{
public TestMemory(){
String test="aaa";
}
}
private static void AddtoList(List list)
{
for(int i=0; i<100000; i++)
list.add(new TestMemory());
}
public static void main(String[] args) {
List list = new java.util.ArrayList(9000000);
int counter = 0;
while(counter <= 10)
{
counter += 1;
AddtoList(list);
Runtime run = Runtime.getRuntime();
double total = run.totalMemory();
double free = run.freeMemory();
double used = (total - free)/(1024*1024);
java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
System.out.println("已使用内存 = " + df.format(used));
ThreadUtil.sleep(1000);
}
System.out.println("目前数量:" + list.size());
for(TestMemory t : list) t=null;
list.clear();
list = null;
while(true)
{
Runtime run = Runtime.getRuntime();
double total = run.totalMemory();
double free = run.freeMemory();
double used = (total - free)/(1024*1024);
java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
System.out.println("已使用内存 = " + df.format(used));
ThreadUtil.sleep(1000);
}
}