java内存管理

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

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

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

资源描述

JavaMemoryManagementOverviewJustinChenMar16rd,2009JustinChen@gmail.com转载请注明原作者March25th,2009BeforeStart…LearningHOWismoreimportantthanlearningWHAT.知其然,更要知其所以然AgendaHowJavaObjectStoresinMemoryShallowSize,RetainedSizeandWeakReferenceJVMMemoryStructure&HeapDumpHowGCworksGCAlgorithmHintsonGCTurningVisualVMandGCViewerOutofMemory–3typesMemoryLeak,PermanentMemoryLeak,NativeMemoryLeakMemoryLeak&EclipseMemoryAnalyzerJDK6Java平台的逻辑结构JVM自身的物理结构Java代码编译和执行的整个过程Java代码编译是由Java源码编译器来完成Java代码编译和执行的整个过程Java字节码的执行是由JVM执行引擎来完成类加载机制1)BootstrapClassLoader负责加载$JAVA_HOME中jre/lib/rt.jar里所有的class,由C++实现,不是ClassLoader子类2)ExtensionClassLoader负责加载java平台中扩展功能的一些jar包,包括$JAVA_HOME中jre/lib/*.jar或-Djava.ext.dirs指定目录下的jar包3)AppClassLoader负责记载classpath中指定的jar包及目录中class4)CustomClassLoader属于应用程序根据自身需要自定义的ClassLoader,如tomcat、jboss都会根据j2ee规范自行实现ClassLoader加载过程中会先检查类是否被已加载,检查顺序是自底向上,从CustomClassLoader到BootStrapClassLoader逐层检查,只要某个classloader已加载就视为已加载此类,保证此类只所有ClassLoader加载一次。而加载的顺序是自顶向下,也就是由上层来逐层尝试加载此类。Java源码编译机制最后生成的class文件由以下部分组成:1.结构信息。包括class文件格式版本号及各部分的数量与大小的信息2.元数据。对应于Java源码中声明与常量的信息。包含类/继承的超类/实现的接口的声明信息、域与方法声明信息和常量池3.方法信息。对应Java源码中语句和表达式对应的信息。包含字节码、异常处理器表、求值栈与局部变量区大小、求值栈的类型记录、调试符号信息Java源码编译由以下三个过程组成:1.分析和输入到符号表2.注解处理3.语义分析和生成class文件类执行机制JVM是基于栈的体系结构来执行class字节码的。线程创建后,都会产生程序计数器(PC)和栈(Stack),程序计数器存放下一条要执行的指令在方法内的偏移量,栈中存放一个个栈帧,每个栈帧对应着每个方法的每次调用,而栈帧又是有局部变量区和操作数栈两部分组成,局部变量区用于存放方法中的局部变量和参数,操作数栈中用于存放方法执行过程中产生的中间结果。栈的结构如下图所示:SizeofJavaTypes&ObjectsFromJavaSpec-byte:8-bitshort:16-bitInt:32-bitlong:64-bitchar:16bitunsignedintegerfloat:32-bitdouble:64-bitboolean:1bit?ActualMemorySize-(dependsonJVMImplementation)Byte:16bytesShort:16bytesInteger:16bytesLong:16bytesCharacter:16bytesFloat:16bytesDouble:16bytesBoolean:16bytesSo,howaboutObject?-newObject()-newnewObject()=8bytesnewDouble[]itself=16bytesnewArrayListDoubleitself=24bytesWhy?MemoryLayoutofBoolean[HEADER:8bytes]8[value:1byte]9[padding:7bytes]16ShallowandRetainedSizesShallowSize:theamountofallocatedmemorytostoretheobjectitself.newArrayListBoolean()=24bytesRetainedSize:shallowsize+theshallowsizesoftheobjectsthatareaccessible,directlyorindirectly,onlyfromthisobject.Inotherwords,theretainedsizerepresentstheamountofmemorythatwillbefreedbythegarbagecollectorwhenthisobjectiscollected.newArrayListBoolean()=80bytesWhat’sGCroot?WeakReferenceWhenstrongreferencearetoostrongStringBufferbuffer=newStringBuffer();hashMap.put(key,value)cache.add(image)Non-StrongReferenceAnobjectreferencedonlybyweakreferencesisconsideredunreachable(orweaklyreachable)andsomaybecollectedatanytime.ReferenceStrength:strongsoftweakphantomSoftReferenceMostlyoftenusedtoimplementmemory-sensitivecachesNoconstraintsonGCtime,butwillbeclearedbeforeOOMWeakReferenceWeaklyreachableobjectwillbediscardedatthenextGCcyclePhantomReferenceAnobjectisphantomlyreferencedafterithasbeenfinalized,butbeforeitsallocatedmemoryhasbeenreclaimed.HeapDumpTheheapdumpisadumpofalltheliveobjectsandclasses.Example:java_pid460.hprof.20090314.234647ThewaystogetHeapDump(SunHotspotJVM5.0update16)-XX:+HeapDumpOnOutOfMemoryError-XX:+HeapDumpOnCtrlBreakuseprofilingtool,suchasVisualVMYouneedatooltoanalyzeheapdumpRuntimeDiagram,VisualVMPost-mortemanalysis,EclipseMemoryAnalyzerUserHeapJVMMemoryStructurePermanentNativeAllclassinstancesArrays-XmxsizemaxNew/Oldheapsize-XmssizeinitialNew/Oldheapsizeper-classstructures:run-timeconstantpool,fieldandmethoddatacodesformethodsandconstructorsInternedString-XX:MaxPermSizeEdenS0S1Old(tenured)GC-ableHeap?ThememorymanagedbyOSNew/YoungwhatisinternedString?HowGCworks?GCofSerialCollectionEmptyEmptySurvivorSpacesOldEdenFromToHowGCworks?FullGCofSerialCollectionFullGC:Aoldorpermanentgenerationcollection(Stoptheworld)BeforeFullGCAfterFullGCGCAlgorithmSerialCollectoraswedescribedinpreviousslidesdefaultcollectorforclient-classserverParallelCollectorPerformingtheyounggenerationcollectioninparallel,oldgenerationissamedefaultcollectorforserver-classserver-XX:+UseParallelGCParallelCompactingCollectorPerformingtheoldgenerationcollectioninparallelcomparingtoparalleccollectorEventually,thiscollectorwillreplaceParallelCollectorinthefuture-XX:+UseParallelOldGCConcurrentMark-Sweep(CMS)Collector(low-latencycollector)ShorterGCpauses,butneedlargerheapspaceandmaycausefragmentation-XX:+UseConcMarkSweepGCGCAlgorithmPerformanceMetricsThroughput–HighisbetterpercentageoftotaltimenotspentinGCGarbageCollectionOverheadpercentageoftotaltimespentinGCPauseTime–LowisbetterthelengthofStopTimeforGCFrequencyofcollectionhowoftencollectionoccursFootprintameasureofsize,suchasheapsizePromptnessThetimebetweenanobjectbecomesgarbageandwhenthememorybecomesavailableHintsonGCTurningEnoughHeapSizeTheproportionofHeapdedicatedtotheyounggenerationgrantplentyofmemorytoyounggenerationmaycauseexcessiveoldgenerationcollectionsorpausetimeSpecifydesiredbehaviourforparallelcollectororparallelcompactingcollector-XX:MaxGCPauseMillis=n-XX:GCTimeRatio=n1/(1+n)DiagnosingaGa

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

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

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

×
保存成功