1、Java Memory Management Overview,Justin Chen Mar 16rd, 2009,Justin Chen http:/ ,转载请注明原作者 March 25th, 2009,Before Start ,Learning HOW is more important than learning WHAT. 知其然,更要知其所以然,Agenda,How Java Object Stores in Memory Shallow Size, Retained Size and Weak Reference JVM Memory Structure & Heap Dum
2、p How GC works GC Algorithm Hints on GC Turning VisualVM and GCViewer Out of Memory 3 types Memory Leak, Permanent Memory Leak, Native Memory Leak Memory Leak & Eclipse Memory Analyzer JDK 6,Java平台的逻辑结构,JVM自身的物理结构,Java代码编译和执行的整个过程,Java代码编译是由Java源码编译器来完成,Java代码编译和执行的整个过程,Java字节码的执行是由JVM执行引擎来完成,类加载机制,
3、1)Bootstrap ClassLoader 负责加载$JAVA_HOME中jre/lib/rt.jar里所有的class,由C+实现,不是ClassLoader子类 2)Extension ClassLoader 负责加载java平台中扩展功能的一些jar包,包括$JAVA_HOME中jre/lib/*.jar或-Djava.ext.dirs指定目录下的jar包 3)App ClassLoader 负责记载classpath中指定的jar包及目录中class 4)Custom ClassLoader 属于应用程序根据自身需要自定义的ClassLoader,如tomcat、jboss都会根据
4、j2ee规范自行实现ClassLoader加载过程中会先检查类是否被已加载,检查顺序是自底向上,从Custom ClassLoader到BootStrap ClassLoader逐层检查,只要某个classloader已加载就视为已加载此类,保证此类只所有ClassLoader加载一次。而加载的顺序是自顶向下,也就是由上层来逐层尝试加载此类。,Java源码编译机制,最后生成的class文件由以下部分组成:1.结构信息。包括class文件格式版本号及各部分的数量与大小的信息2.元数据。对应于Java源码中声明与常量的信息。包含类/继承的超类/实现的接口的声明信息、域与方法声明信息和常量池3.方法
5、信息。对应Java源码中语句和表达式对应的信息。包含字节码、异常处理器表、求值栈与局部变量区大小、求值栈的类型记录、调试符号信息,Java 源码编译由以下三个过程组成: 1.分析和输入到符号表 2.注解处理 3.语义分析和生成class文件,类执行机制,JVM是基于栈的体系结构来执行class字节码的。线程创建后,都会产生程序计数器(PC)和栈(Stack),程序计数器存放下一条要执行的指令在方法内的偏移量,栈中存放一个个栈帧,每个栈帧对应着每个方法的每次调用,而栈帧又是有局部变量区和操作数栈两部分组成,局部变量区用于存放方法中的局部变量和参数,操作数栈中用于存放方法执行过程中产生的中间结果。
6、栈的结构如下图所示:,Size of Java Types & Objects,From Java Spec - byte : 8-bit short : 16-bit Int : 32-bit long : 64-bit char : 16 bit unsigned integerfloat : 32-bit double: 64-bit boolean: 1 bit?,Actual Memory Size - (depends on JVM Implementation) Byte : 16 bytes Short : 16 bytes Integer : 16 bytes Long :
7、16 bytes Character : 16 bytesFloat : 16 bytes Double : 16 bytes Boolean : 16 bytes,So, how about Object? new Object() new,new Object() = 8 bytes new Double itself = 16 bytesnew ArrayList itself = 24 bytes,Why? Memory Layout of Boolean HEADER: 8 bytes 8 value: 1 byte 9 padding: 7 bytes 16,Shallow and
8、 Retained Sizes,Shallow Size : the amount of allocated memory to store the object itself. new ArrayList() = 24 bytes,Retained Size : shallow size + the shallow sizes of the objects that are accessible, directly or indirectly, only from this object. In other words, the retained size represents the am
9、ount of memory that will be freed by the garbage collector when this object is collected. new ArrayList() = 80 bytes,Whats GC root?,Weak Reference,When strong reference are too strong StringBuffer buffer = new StringBuffer(); hashMap.put(key, value) cache.add(image) Non-Strong Reference An object re
10、ferenced only by weak references is considered unreachable (or “weakly reachable“) and so may be collected at any time. Reference Strength: strong soft weak phantom Soft Reference Mostly often used to implement memory-sensitive caches No constraints on GC time, but will be cleared before OOM Weak Re
11、ference Weakly reachable object will be discarded at the next GC cycle Phantom Reference An object is phantomly referenced after it has been finalized, but before its allocated memory has been reclaimed.,Heap Dump,The heap dump is a dump of all the live objects and classes. Example: java_pid460.hpro
12、f.20090314.234647 The ways to get Heap Dump (Sun Hotspot JVM 5.0 update 16) -XX:+HeapDumpOnOutOfMemoryError -XX:+HeapDumpOnCtrlBreak use profiling tool, such as VisualVM You need a tool to analyze heap dump Runtime Diagram, Visual VM Post-mortem analysis, Eclipse Memory Analyzer,User Heap,JVM Memory
13、 Structure,Permanent,Native,All class instances Arrays -Xmx max New/Old heap size -Xms initial New/Old heap size,per-class structures: run-time constant pool, field and method data codes for methods and constructors Interned String -XX:MaxPermSize,Eden,S0,S1,Old (tenured),GC-able Heap?,The memory ma
14、naged by OS,what is interned String?,How GC works? GC of Serial Collection,New Object,How GC works? Full GC of Serial Collection,Full GC: A old or permanent generation collection (Stop the world),GC Algorithm,Serial Collector as we described in previous slides default collector for client-class serv
15、er Parallel Collector Performing the young generation collection in parallel, old generation is same default collector for server-class server -XX:+UseParallelGC Parallel Compacting Collector Performing the old generation collection in parallel comparing to parallec collectorEventually, this collect
16、or will replace Parallel Collector in the future -XX:+UseParallelOldGC Concurrent Mark-Sweep (CMS) Collector (low-latency collector) Shorter GC pauses, but need larger heap space and may cause fragmentation -XX:+UseConcMarkSweepGC,GC Algorithm Performance Metrics,Throughput High is better percentage
17、 of total time not spent in GC Garbage Collection Overhead percentage of total time spent in GC Pause Time Low is better the length of Stop Time for GC Frequency of collection how often collection occurs Footprint a measure of size, such as heap size Promptness The time between an object becomes gar
18、bage and when the memory becomes available,Hints on GC Turning,Enough Heap Size The proportion of Heap dedicated to the young generation grant plenty of memory to young generation may cause excessive old generation collections or pause time Specify desired behaviour for parallel collector or paralle
19、l compacting collector -XX:MaxGCPauseMillis=n -XX:GCTimeRatio=n 1/(1+n) Diagnosing a Garbage Collection problem http:/ Log,-Xloggc:d:gc.log -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps add time stamp,3603.329: GC PSYoungGen: 244309K-4520K(244800K) 705465K-472071K(978816K), 0.0274399 secs,
20、Young Gen: before GC-After GC,All: before GC-after GC,Pause Time,3603.357: Full GC PSYoungGen: 4520K-0K(244800K)PSOldGen: 467550K-407305K(734016K) 472071K-407305K(978816K) PSPermGen: 81712K-81712K(171136K), 1.3827685 secs,Visual VM,Get Runtime Heap Dump Monitor Visual GC,time spent compiling Java by
21、te codes into native code,TenuringThreshold & MaxTenuringThreshold also impacts performance,GC Viewer - Post-mortem analysis,Out of Memory - java.lang.OutOfMemoryError,Java heap space Configuration issue: -Xmx Memory Leak The excessive use of finalizers PermGen space Too many classes: -XX:MaxPermSiz
22、e Requested array size exceeds VM limit Why need a so big array? Request bytes for . Out of swap space? Native Memory Leak?(Native method) Native Memory Allocation Issue,Memory Leak vs. OOM,Memory Leak (OOM != Memory Leak) An unintentional memory usage. Java memory leaks are objects which are not us
23、ed/needed anymore, but which are still reachable and therefore are not removed by the Garbage Collector. How Can We Find Leak In most of cases, the biggest objects Who Caused Leak? follow the reference chain from the object to the (GC) roots GC Roots Objects on the call stack of the current thread (
24、e.g. method parameters and local variables) The thread itself and classes loaded by the system class loader custom class loaders are NOT,Eclipse Memory Analysis Heap Memory Leak,Perm Memory Leak,Too Many Interned String String.intern() Constant String will be interned implicitly No Enough Info provi
25、ded by Heap Dump on Interned String If Perm Memory increased dynamically, be careful Too Many Classes or Class Load Leak Enlarge the perm generation Avoid duplicated class loader,Eclipse Memory Analyzer Perm Generation,Class Loader Explorer: Java Basic = Class Load Explorer,Class Loader Name,Loaded
26、Class Number,Loaded Object Number,List Duplicated Class Loaders,Java Basic = Duplicated Classes,Out of Swap Space?,Request bytes for . Out of swap space? Enlarge the swap space Systems with 4GB of ram or less require a minimum of 2GB of swap space Systems with 4GB to 16GB of ram require a minimum of
27、 4GB of swap space For Unix Family OS, use pmdump or pmap, libumem for Solaris,# An unexpected error has been detected by SAP Java Virtual Machine: # java.lang.OutOfMemoryError: requested 2048000 bytes for eArray.cpp:80: GrET*.Out of swap space or heap resource limit exceeded (check with limits or u
28、limit)? # Internal Error (.hotspotsrcsharevmmemoryallocation.inline.hpp, 26), pid=6000, tid=468,Native Memory Leak?, (Native method) Native Memory Allocation Issue C heap exhaustion is usually the result of a memory leak in either VM itself Core library native code Third party native code Append -ve
29、rbose:jni option after JVM args. Observe the loading and linking actions of jni dynamic libs No many choices Check JVM bugs,JDK 6 ! Performance Enhancement,Runtime Performance Improvement Synchronization & Multiple Threads Array Copy Background compilation (Runtime Code Optimization) GC Performance
30、Improvement Parallel Compaction Collector Concurrent Low Pause Collector,Last but not least,Lets use powerful machine 64 bit 4 - 8 CPU 16GB 64GB Memory Unix Family OS Why Java Runs on Windows? Solaris HP-UX Unit Linux However, Hardware cannot help you survive if the codes are rubbish!,References,http:/ http:/wiki.eclipse.org/index.php/MemoryAnalyzer http:/dev.eclipse.org/blogs/memoryanalyzer/2008/04/21/immortal-objects-or-how-to-find-memory-leaks/ http:/ http:/ http:/ http:/ http:/ http:/ http:/ http:/