public class MemoryLeak {
public static void main(String[] args) {
//由于线程一直在运行,因此ThreadLocalMap里的Inner对象一直被Thread对象强引用
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
//每次都新建一个ClassLoader实例去加载Inner类
CustomClassLoader classLoader = new CustomClassLoader
("load1", MemoryLeak.class.getClassLoader(), "com.ezlippi.MemoryLeak$Inner", "com.ezlippi.MemoryLeak$Inner$1");
try {
Class<?> innerClass = classLoader.loadClass("com.ezlippi.MemoryLeak$Inner");
innerClass.newInstance();
//帮助GC进行引用处理
innerClass = null;
classLoader = null;
Thread.sleep(10);
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException | InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
//为了更快达到堆区
public static class Inner {
private byte[] MB = new byte[1024 * 1024];
static ThreadLocal<Inner> threadLocal = new ThreadLocal<Inner>() {
@Override
protected Inner initialValue() {
return new Inner();
}
};
//调用ThreadLocal.get()才会调用initialValue()初始化一个Inner对象
static {
threadLocal.get();
}
public Inner() {
}
}
//源码省略
private static class CustomClassLoader extends ClassLoader {}
-Xms100m -Xmx100m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintHeapAtGC -XX:+PrintClassHistogram -XX:+HeapDumpOnOutOfMemoryError
java.lang.OutOfMemoryError: Java heap space Dumping heap to java_pid11824.hprof ... Heap dump file created [100661202 bytes in 1.501 secs] Heap par new generation total 30720K, used 30389K [0x00000000f9c00000, 0x00000000fbd50000, 0x00000000fbd50000) eden space 27328K, 99% used [0x00000000f9c00000, 0x00000000fb6ad450, 0x00000000fb6b0000) from space 3392K, 90% used [0x00000000fb6b0000, 0x00000000fb9b0030, 0x00000000fba00000) to space 3392K, 0% used [0x00000000fba00000, 0x00000000fba00000, 0x00000000fbd50000) concurrent mark-sweep generation total 68288K, used 67600K [0x00000000fbd50000, 0x0000000100000000, 0x0000000100000000) Metaspace used 3770K, capacity 5134K, committed 5248K, reserved 1056768K class space used 474K, capacity 578K, committed 640K, reserved 1048576K Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space at com.ezlippi.MemoryLeak$Inner.<clinit>(MemoryLeak.java:34) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at com.ezlippi.MemoryLeak$1.run(MemoryLeak.java:20) at java.lang.Thread.run(Unknown Source)
num #instances #bytes class name ---------------------------------------------- 1: 6203 855158648 [B 2: 13527 1487984 [C 3: 298 700560 [I 4: 2247 228792 java.lang.Class 5: 8232 197568 java.lang.String 6: 3095 150024 [Ljava.lang.Object; 7: 1649 134480 [Ljava.util.HashMap$Node; 11: 813 65040 com.ezlippi.MemoryLeak$CustomClassLoader 12: 820 53088 [Ljava.util.Hashtable$Entry; 15: 817 39216 java.util.Hashtable 16: 915 36600 java.lang.ref.SoftReference 17: 543 34752 java.net.URL 18: 697 33456 java.nio.HeapCharBuffer 19: 817 32680 java.security.ProtectionDomain 20: 785 31400 java.util.TreeMap$Entry 21: 928 29696 java.util.Hashtable$Entry 22: 1802 28832 java.util.HashSet 23: 817 26144 java.security.CodeSource 24: 814 26048 java.lang.ThreadLocal$ThreadLocalMap$Entry
private byte[] KB = new byte[1024]; -Xms100m -Xmx100m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintHeapAtGC -XX:+PrintClassHistogram -XX:MetaspaceSize=2m -XX:MaxMetaspaceSize=2m
java.lang.OutOfMemoryError: Metaspace
<<no stack trace available>>
{Heap before GC invocations=20 (full 20):
par new generation total 30720K, used 0K [0x00000000f9c00000, 0x00000000fbd50000, 0x00000000fbd50000)
eden space 27328K, 0% used [0x00000000f9c00000, 0x00000000f9c00000, 0x00000000fb6b0000)
from space 3392K, 0% used [0x00000000fb6b0000, 0x00000000fb6b0000, 0x00000000fba00000)
to space 3392K, 0% used [0x00000000fba00000, 0x00000000fba00000, 0x00000000fbd50000)
concurrent mark-sweep generation total 68288K, used 432K [0x00000000fbd50000, 0x0000000100000000, 0x0000000100000000)
Metaspace used 1806K, capacity 1988K, committed 2048K, reserved 1056768K
class space used 202K, capacity 384K, committed 384K, reserved 1048576K
[Full GC (Metadata GC Threshold) [CMS
Process finished with exit code 1
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有