62,621
社区成员
发帖
与我相关
我的任务
分享class MemoryTest2
{
public static void main(String[] args)
{
StringBuffer s2=new StringBuffer("abcddafdfadsfad");
System.out.println("当前虚拟机最大可用内存:");
System.out.println(Runtime.getRuntime().maxMemory()/1024/1024+"M");
System.out.println("循环前内存:");
System.out.println(Runtime.getRuntime().maxMemory()/1024/1024+"M");
int count=0;
while(true)
{
try
{
s2.append(s2);
count++;
}catch(Error o){
System.out.println("循环次数:"+count);
System.out.println("字节数:"+s2.length()/1024/1024+"M");
System.out.println("循环后内存:");
System.out.println(Runtime.getRuntime().maxMemory()/1024/1024+"M");
System.out.println("Catch到的错误:"+o);
break;
}
}
}
}