拓胜技术专家教你如何深入理解Java内存管理

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1/25JavaC++C++——Javanewdelete/freeJVMJavaJVMJVMVMJVMJavaJavaVMSpecJVM1.ProgramCounterRegisterJavaNativeVMNatvieundefinedVMSpecOutOfMemoryError2.JavaJavaVirtualMachineStacksVMVMJavaFrame2/25VMVMJavaHeapStackVMbooleanbytecharshortintfloatlongdoublelongdouble32bitVMSpecStackOverflowErrorVMVMSpecVMOutOfMemoryError3.NativeMethodStacksVMVMVMNativeSunHotspotVMVMStackOverflowErrorOutOfMemoryError4.JavaJavaHeapJavaJavaJavaVMSpecTheheapistheruntimedataareafromwhichmemoryforallclassinstancesandarraysisallocatedVMSpecJavaedenfromsurvivortosurvivorTLABJavaJavaJVMVMSpecJava-Xmx-XmsOutOfMemoryError5.MethodArea3/25PermanentGenerationNon-HeapVMSpecthemethodareaislogicallypartoftheheapClassVMSpaceJavaGCJVMGC6.RuntimeConstantPoolClass(constant_pooltable)JavaClassString.intern()OutOfMemoryError7.DirectMemoryVMOutOfMemoryErrorJDK1.4NIOI/ONativeJavaDirectByteBufferJavaJavaSWAPWindowsJVM-XmxOutOfMemoryErrorOutOfMemoryError4/25VMSpecOutOfMemoryErrorOOMOOMSunHotspot1.6JavaJavaGCRootsOOMJava20M-XX:+HeapDumpOnOutOfMemoryErrorOOMDumpDumpJVMJVMJavaOOM/***VMArgs-Xms20m-Xmx20m-XX:+HeapDumpOnOutOfMemoryError*@authorzzm*/publicclassHeapOOM{staticclassOOMObject{}publicstaticvoidmain(String[]args){ListOOMObjectlist=newArrayListOOMObject();5/25while(true){list.add(newOOMObject());}}}java.lang.OutOfMemoryError:JavaheapspaceDumpingheaptojava_pid3404.hprof...Heapdumpfilecreated[22045981bytesin0.663secs]VMHotspotVM-Xoss-XssVMVMSpecStackOverflowErrorOutOfMemoryErrorOOMSOF1.-XssSOF2.SOF3.JIT6/25VMOOM/***VMArgs-Xss128k*@authorzzm*/publicclassJavaVMStackSOF{privateintstackLength=1;publicvoidstackLeak(){stackLength++;stackLeak();}publicstaticvoidmain(String[]args)throwsThrowable{JavaVMStackSOFoom=newJavaVMStackSOF();try{oom.stackLeak();}catch(Throwablee){System.out.println(stacklength:+oom.stackLength);throwe;}7/25}}stacklength:2402Exceptioninthreadmainjava.lang.StackOverflowErroratorg.fenixsoft.oom.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:20)atorg.fenixsoft.oom.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:21)atorg.fenixsoft.oom.JavaVMStackSOF.stackLeak(JavaVMStackSOF.java:21)OOMVMVMOOM32Windows2GJavaJVM2G-Xmx-MaxPermSizeVMVMOOM/***VMArgs-Xss2M*@authorzzm8/25*/publicclassJavaVMStackOOM{privatevoiddontStop(){while(true){}}publicvoidstackLeakByThread(){while(true){Threadthread=newThread(newRunnable(){@Overridepublicvoidrun(){dontStop();}});thread.start();}}publicstaticvoidmain(String[]args)throwsThrowable{JavaVMStackOOMoom=newJavaVMStackOOM();9/25oom.stackLeakByThread();}}Exceptioninthreadmainjava.lang.OutOfMemoryError:unabletocreatenewnativethreadString.intern()Native-XX:PermSize-XX:MaxPermSizeOOM/***VMArgs-XX:PermSize=10M-XX:MaxPermSize=10M*@authorzzm*/publicclassRuntimeConstantPoolOOM{publicstaticvoidmain(String[]args){10/25//ListFullGCListStringlist=newArrayListString();//10MPermSizeintegerOOMinti=0;while(true){list.add(String.valueOf(i++).intern());}}}Exceptioninthreadmainjava.lang.OutOfMemoryError:PermGenspaceatjava.lang.String.intern(NativeMethod)atorg.fenixsoft.oom.RuntimeConstantPoolOOM.main(RuntimeConstantPoolOOM.java:18)ClassCGLibClassSpringHibernateCGLibClass11/25CGLibOOM/***VMArgs-XX:PermSize=10M-XX:MaxPermSize=10M*@authorzzm*/publicclassJavaMethodAreaOOM{publicstaticvoidmain(String[]args){while(true){Enhancerenhancer=newEnhancer();enhancer.setSuperclass(OOMObject.class);enhancer.setUseCache(false);enhancer.setCallback(newMethodInterceptor(){publicObjectintercept(Objectobj,Methodmethod,Object[]args,MethodProxyproxy)throwsThrowable{returnproxy.invokeSuper(obj,args);}});enhancer.create();}}12/25staticclassOOMObject{}}Causedby:java.lang.OutOfMemoryError:PermGenspaceatjava.lang.ClassLoader.defineClass1(NativeMethod)atjava.lang.ClassLoader.defineClassCond(ClassLoader.java:632)atjava.lang.ClassLoader.defineClass(ClassLoader.java:616)...8moreDirectMemory-XX:MaxDirectMemorySizeJava-XmxDirectByteBufferUnsafeUnsafegetUnsafe()rt.jarDirectByteBufferOOMunsafe.allocateMemory()/***VMArgs-Xmx20M-XX:MaxDirectMemorySize=10M*@authorzzm13/25*/publicclassDirectMemoryOOM{privatestaticfinalint_1MB=1024*1024;publicstaticvoidmain(String[]args)throwsException{FieldunsafeField=Unsafe.class.getDeclaredFields()[0];unsafeField.setAccessible(true);Unsafeunsafe=(Unsafe)unsafeField.get(null);while(true){unsafe.allocateMemory(_1MB);}}}Exceptioninthreadmainjava.lang.OutOfMemoryErroratsun.misc.Unsafe.allocateMemory(NativeMethod)atorg.fenixsoft.oom.DirectMemoryOOM.main(DirectMemoryOOM.java:20)14/25OOMJavaOOMOOMJavaOOM*********************************************************************************************************************************************************************************************************************************************JavaC++GarbageCollectionGCJavaGCJava1960MITLispLispGCGC1960JavaJavaVMClassJITJavaJava15/25ReferenceCountingA.b=BB.a=AGCRootsTracingJavaC#Lisp“GCRoots”GCRootsReferenceChainJavaGCRoots1.VM2.3.JNINativeGCRootsfinalize()finalize()finalize()GCfinalize()GCRootsfinalize()C/C++try-finallyGCJavaGCGC16/25GC70%~95%GCVMSpecJVMGCJav

1 / 25
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功