内存溢出
内存溢出:
操作系统可提供给所有进程的存储空间正在被某个进程榨干,耗尽内存没找成整个系统崩溃;
内存溢出会抛异常:Java.lang.OutOfMemoryError
演示代码:
import Java.util.HashMap; import Java.util.Map; public class MemoryLeakDemo { static class Key { Integer id; Key(Integer id) { this.id = id; } @Override public int hashCode() { return id.hashCode(); } } public static void main(String[] args) { Map m = new HashMap(); while (true) for (int i = 0; i < 10000; i++) if (!m.containsKey(new Key(i))) m.put(new Key(i), "Number:" + i); } }
输出:
Exception in thread "main" Java.lang.OutOfMemoryError: GC overhead limit exceeded
at Java.util.HashMap.newNode(Unknown Source)
at Java.util.HashMap.putVal(Unknown Source)
at Java.util.HashMap.put(Unknown Source)
at MemoryLeakDemo.main(MemoryLeakDemo.Java:23)