常见的内存泄露:
1.文件流不关闭
2.数据库连接没有关闭
3.内存使用过多
内存不够用如下:
import java.util.ArrayList;
import java.util.List;
public class TestMemoryLeak {
public static void main(String[] args) throws InterruptedException {
List<int[]> list = new ArrayList<int[]>();
Runtime run = Runtime.getRuntime();
int i=1;
while(true){
int[] arr = new int[1024 * 8];
list.add(arr);
if(i++ % 1000 == 0 ){
System.out.print("最大内存=" + run.maxMemory() / 1024 / 1024 + "M,");
System.out.print("已分配内存=" + run.totalMemory() /1024 / 1024 + "M,");
System.out.print("剩余空间内存=" + run.freeMemory() / 1024 / 1024 + "M");
System.out.println("最大可用内存=" + ( run.maxMemory() - run.totalMemory() + run.freeMemory() ) / 1024 / 1024 + "M");
}
}
}
}