62,614
社区成员
发帖
与我相关
我的任务
分享
public class Test {
public static void main(String[] args) throws Throwable {
List<int[]> list = new ArrayList<int[]>();
int m = 1024*1024*16; //64M内存 因为int是4个字节 4*16=64
int count = 0;
while (true) {
try {
list.add(new int[m]); //每次申请64M内存
System.out.println(count++);
} catch (Throwable e) {
break; //抛出内存溢出则停止申请内存
}
}
new Thread() {
public void run() {
while (true) { //用一个线程来保持程序不结束,可以在任务管理器查看内存使用量
try {yield();} catch (Exception e) {}
}
}
}.start();
} //强行中断线程,当程序结束以后再查看内存使用量
}
List<int[]> list = new ArrayList<int[]>();
int m = 1024*1024*16; //64M内存
int count = 0;
while (true) {
list.add(new int[m]);
System.out.println(count++); //看看能运行几次就没内存了
}