一、定义
内存泄露:某些对象使用后,没有被释放掉,又gc不掉,导致一直占用内存。
二、实例
/**
* -Xmx10m -Xms10m -XX:+PrintGCDetails
* @author scipio
* @created 2014-09-03
*/
public class MemoryLeakDemo2 {
final static ExecutorService exec= Executors.newFixedThreadPool(10);
public static void main(String[] args){
MemoryLeakDemo.printMemoryInfo();
Future<Boolean> future = exec.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
//放到thread local ,同时不关闭executor
MemoryLeakDemo.cache.get().data = new byte[1024 * 1024 * 5];
//normal
// byte[] bytes = new byte[1024*1024*5];
return true;
}
});
//wait for result
try {
future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("finish");
//release 关闭了可以避免full gc 或者 内存泄露
// exec.shutdown();
// try {
// exec.awaitTermination(5,TimeUnit.SECONDS);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// MemoryLeakDemo.printMemoryInfo();
System.out.println("gc");
System.gc();
MemoryLeakDemo.printMemoryInfo();
}
}