-verbose:gc -Xms20m -Xmx20m -XX: HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\dump
/** * java 堆内存溢出 * <p> * VM Args: -Xms20m -Xmx20m -XX: HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\dump * * @author yuhao.wang3 */ public class HeapOutOfMemoryErrorTest { public static void main(String[] args) throws InterruptedException { // 模拟大容器 List<Object> list = Lists.newArrayList(); for (long i = 1; i > 0; i ) { list.add(new Object()); if (i % 100_000 == 0) { System.out.println(Thread.currentThread().getName() "::" i); } } } }
[GC (Allocation Failure) 5596K->1589K(19968K), 0.0422027 secs] main::100000 main::200000 [GC (Allocation Failure) 7221K->5476K(19968K), 0.0144103 secs] main::300000 [GC (Allocation Failure) 9190K->9195K(19968K), 0.0098252 secs] main::400000 main::500000 [Full GC (Ergonomics) 17992K->13471K(19968K), 0.3431052 secs] main::600000 main::700000 main::800000 [Full GC (Ergonomics) 17127K->16788K(19968K), 0.1581969 secs] [Full GC (Allocation Failure) 16788K->16758K(19968K), 0.1994445 secs] java.lang.OutOfMemoryError: Java heap space Dumping heap to D:\dump\java_pid7432.hprof ... Heap dump file created [28774262 bytes in 0.221 secs] Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3210) at java.util.Arrays.copyOf(Arrays.java:3181) at java.util.ArrayList.grow(ArrayList.java:261) at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235) at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227) at java.util.ArrayList.add(ArrayList.java:458) at com.xiaolyuh.HeapOutOfMemoryErrorTest.main(HeapOutOfMemoryErrorTest.java:23) Disconnected from the target VM, address: '127.0.0.1:61622', transport: 'socket'
[Full GC (Ergonomics) 19225K->19225K(19968K), 0.1044070 secs] [Full GC (Ergonomics) 19227K->19227K(19968K), 0.0684710 secs] java.lang.OutOfMemoryError: GC overhead limit exceeded Dumping heap to D:\dump\java_pid17556.hprof ... Heap dump file created [34925385 bytes in 0.132 secs] Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded [Full GC (Ergonomics) 19257K->933K(19968K), 0.0403569 secs] at com.xiaolyuh.HeapOutOfMemoryErrorTest.main(HeapOutOfMemoryErrorTest.java:25) ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2 JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [util.c:840]
Collections.unmodifiableList( Collections.unmodifiableList( Collections.unmodifiableList( Collections.unmodifiableList( Collections.unmodifiableList( ...)))))));
/** * java 虚拟机栈和本地方法栈内存溢出测试 * <p> * VM Args: -Xss128k * * @author yuhao.wang3 */ public class StackOverflowErrorErrorTest { private int stackLength = 0; public void stackLeak() { stackLength ; stackLeak(); } public static void main(String[] args) { StackOverflowErrorErrorTest sof = new StackOverflowErrorErrorTest(); try { sof.stackLeak(); } catch (Exception e) { System.out.println(sof.stackLength); e.printStackTrace(); } } }
stackLength::1372 java.lang.StackOverflowError at com.xiaolyuh.StackOverflowErrorErrorTest.stackLeak(StackOverflowErrorErrorTest.java:16) at com.xiaolyuh.StackOverflowErrorErrorTest.stackLeak(StackOverflowErrorErrorTest.java:16) at com.xiaolyuh.StackOverflowErrorErrorTest.stackLeak(StackOverflowErrorErrorTest.java:16) ...
stackLength::20641 java.lang.StackOverflowError at com.xiaolyuh.StackOverflowErrorErrorTest.stackLeak(StackOverflowErrorErrorTest.java:16) at com.xiaolyuh.StackOverflowErrorErrorTest.stackLeak(StackOverflowErrorErrorTest.java:16) ...
public void stackLeak(String ags1, String ags2, String ags3) { stackLength ; stackLeak(ags1, ags2, ags3); }
stackLength::13154 java.lang.StackOverflowError at com.xiaolyuh.StackOverflowErrorErrorTest.stackLeak(StackOverflowErrorErrorTest.java:16) at com.xiaolyuh.StackOverflowErrorErrorTest.stackLeak(StackOverflowErrorErrorTest.java:16) ...
/** * java 虚拟机栈和本地方法栈内存溢出测试 * <p> * 创建线程过多导致内存溢出异常 * <p> * VM Args: -verbose:gc -Xss20M -XX: HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\dump * * @author yuhao.wang3 * @since 2019/11/30 17:09 */ public class StackOutOfMemoryErrorTest { private static int threadCount; public static void main(String[] args) throws Throwable { try { while (true) { threadCount ; new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000 * 60 * 10); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } } catch (Throwable e) { e.printStackTrace(); throw e; } finally { System.out.println("threadCount=" threadCount); } } }
java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(Thread.java:717) at StackOutOfMemoryErrorTest.main(StackOutOfMemoryErrorTest.java:17) threadCount=4131 Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread at java.lang.Thread.start0(Native Method) at java.lang.Thread.start(Thread.java:717) at StackOutOfMemoryErrorTest.main(StackOutOfMemoryErrorTest.java:17)
/** * java 元数据区域/方法区的内存溢出 * <p> * VM Args JDK 1.6: set JAVA_OPTS=-verbose:gc -XX:PermSize=10m -XX:MaxPermSize=10m -XX: HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\dump * <p> * VM Args JDK 1.8: set JAVA_OPTS=-verbose:gc -Xmx20m -XX:MetaspaceSize=5m -XX:MaxMetaspaceSize=5m -XX: HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\dump * * @author yuhao.wang3 */ public class MethodAreaOutOfMemoryErrorTest { static class MethodAreaOOM { } public static void main(String[] args) { while (true) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(MethodAreaOOM.class); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] params, MethodProxy proxy) throws Throwable { return proxy.invokeSuper(obj, params); } }); enhancer.create(); } } }
[GC (Last ditch collection) 1283K->1283K(16384K), 0.0002585 secs] [Full GC (Last ditch collection) 1283K->1226K(19968K), 0.0075856 secs] java.lang.OutOfMemoryError: Metaspace Dumping heap to D:\dump\java_pid18364.hprof ... Heap dump file created [2479477 bytes in 0.015 secs] [GC (Metadata GC Threshold) 1450K->1354K(19968K), 0.0003906 secs] [Full GC (Metadata GC Threshold) 1354K->976K(19968K), 0.0073752 secs] [GC (Last ditch collection) 976K->976K(19968K), 0.0002921 secs] [Full GC (Last ditch collection) 976K->973K(19968K), 0.0045243 secs] Exception in thread "main" java.lang.OutOfMemoryError: Metaspace at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:763) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) at java.net.URLClassLoader.access$100(URLClassLoader.java:73) at java.net.URLClassLoader$1.run(URLClassLoader.java:368) at java.net.URLClassLoader$1.run(URLClassLoader.java:362) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:361) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:52) at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:116) at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:291) at org.springframework.cglib.core.KeyFactory$Generator.create(KeyFactory.java:221) at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:174) at org.springframework.cglib.core.KeyFactory.create(KeyFactory.java:153) at org.springframework.cglib.proxy.Enhancer.<clinit>(Enhancer.java:73) at com.xiaolyuh.MethodAreaOutOfMemoryErrorTest.main(MethodAreaOutOfMemoryErrorTest.java:26)
/** * java 方法区和运行时常量池溢出 * <p> * VM Args JDK 1.6: set JAVA_OPTS=-verbose:gc -XX:PermSize10 -XX:MaxPermSize10m -XX: HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\dump * * @author yuhao.wang3 */ public class RuntimeConstantOutOfMemoryErrorTest { public static void main(String[] args) { // 使用List保存着常量池的引用,避免Full GC 回收常量池行为 List<String> list = new ArrayList<>(); for (int i = 0; ; i ) { list.add(String.valueOf(i).intern()); } } }
Exception in thread "main" java.lang.OutOfMemoryError: PermGen space at java.lang.String.intern(Native Method) at RuntimeConstantOutOfMemoryErrorTest.main(RuntimeConstantOutOfMemoryErrorTest.java:18)
/** * java 直接内存溢出 * <p> * VM Args JDK 1.6: set JAVA_OPTS=-verbose:gc -Xms20m -XX:MaxDirectMemorySize=10m -XX: HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\dump * * @author yuhao.wang3 */ public class DirectMemoryOutOfMemoryErrorTest { public static void main(String[] args) throws IllegalAccessException { int _1M = 1024 * 1024; Field unsafeField = Unsafe.class.getDeclaredFields()[0]; unsafeField.setAccessible(true); Unsafe unsafe = (Unsafe) unsafeField.get(null); while (true) { unsafe.allocateMemory(_1M); } } }
Exception in thread "main" java.lang.OutOfMemoryError at sun.misc.Unsafe.allocateMemory(Native Method) at com.xiaolyuh.DirectMemoryOutOfMemoryErrorTest.main(DirectMemoryOutOfMemoryErrorTest.java:23)
END 十期推荐 【221期】面试官:谈谈内存泄漏和内存溢出的联系与区别 【222期】ZooKeeper 相关面试演练 【223期】面试官:在MySQL查询中,为什么要用小表驱动大表 【224期】MySQL索引相关面试演练 【225期】面试官:公司项目中Java的多线程一般用在哪些场景? 【226期】面试官:内存耗尽后Redis会发生什么 【227期】面试官:说说双重检查加锁单例模式为什么两次判断? 【228期】面试高频:Java常用的八大排序算法一网打尽! 【229期】面试官:怎么解决Eureka某一个服务挂掉的问题? 【230期】面试官:讲讲Bean的加载过程 ? ~