堆
/** * jvm 参数: -Xms5m -Xmx5m -Xmn2m -XX:NewSize=1m * @author admin * */ public class HeapOutOfMemoryError { // 堆 @SuppressWarnings("unused") private String[] strings = new String[100000]; public static void main(String[] args) { Map<Object, HeapOutOfMemoryError> map = new HashMap<Object, HeapOutOfMemoryError>(); int i = 0; do { map.put(String.valueOf(i), new HeapOutOfMemoryError()); i++; } while (i<100000); } }
方法区(永久代)
public class PermOutOfMemoryError { // 方法区 @SuppressWarnings("unused") private static String[] strings = new String[100000]; public static void main(String[] args) { Map<Object, PermOutOfMemoryError> map = new HashMap<Object, PermOutOfMemoryError>(); int i = 0; do { map.put(String.valueOf(i), new PermOutOfMemoryError()); i++; } while (i<100000); } }
栈
public class StackOverFlowError { public static void main(String[] args) { int i = 0; go(i); } @SuppressWarnings("unused") private static void go(int i) { System.out.println(i); String[] strings = new String[100000]; i++; go(i); } }