publicclassEntity{ ArrayList<String> arrayList; String name; int age;
publicEntity(String a, int b){ name = a; int len = 100; arrayList = new ArrayList<String>(100); for (int i = 0; i < len; i++) { arrayList.add("thisi is no. " + i); } age = b; } }
publicvoidoom()throws InterruptedException { int len = 30000; Object[] array = new Object[len]; for (int i = 0; i < len; i++) { String d = new Date().toString(); Entity p = new Entity(d, i); Map<Integer, Entity> map = new HashMap<>(); map.put(i, p); array[i] = p; Thread.sleep(1); } } }
➜ target java -Xmx10m -Xms10m -XX:+HeapDumpOnOutOfMemoryError com.ajupiter.Main java.lang.OutOfMemoryError: Java heap space Dumping heap to java_pid33714.hprof ... Heap dump file created [14343715 bytes in 0.080 secs] Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.base/jdk.internal.misc.Unsafe.allocateUninitializedArray(Unsafe.java:1271) at java.base/java.lang.invoke.StringConcatFactory$MethodHandleInlineCopyStrategy.newArray(StringConcatFactory.java:1633) at java.base/java.lang.invoke.DirectMethodHandle$Holder.invokeStatic(DirectMethodHandle$Holder) at java.base/java.lang.invoke.LambdaForm$MH/0x0000000800063c40.invoke(LambdaForm$MH) at java.base/java.lang.invoke.Invokers$Holder.linkToTargetMethod(Invokers$Holder) at com.ajupiter.Entity.<init>(Entity.java:16) at com.ajupiter.OOMProducer.oom(OOMProducer.java:15) at com.ajupiter.Main.main(Main.java:9)