收藏 分销(赏)

java内存管理.ppt

上传人:仙人****88 文档编号:14773775 上传时间:2026-11-19 格式:PPT 页数:34 大小:1.83MB 下载积分:10 金币
下载 相关
java内存管理.ppt_第1页
第1页 / 共34页
java内存管理.ppt_第2页
第2页 / 共34页


点击查看更多>>
资源描述
,Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,*,Java Memory Management Overview,Justin Chen,Mar 16,rd,2009,Justin Chen,25,th,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 Dump,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,执行引擎来完成,类加载机制,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,都会根据,j2ee,规范自行实现,ClassLoader,加载过程,中会先检查类是否被已加载,检查顺序是自底向上,从,Custom ClassLoader,到,BootStrap ClassLoader,逐层检查,只要某个,classloader,已加载就视为已加载此类,保证此类只所有,ClassLoader,加载一次。而加载的顺序是自顶向下,也就是由上层来逐层尝试加载此类。,Java,源码编译机制,最后生成的,class,文件由以下部分组成:,1.,结构信息。包括,class,文件格式版本号及各部分的数量与大小的信息,2.,元数据。对应于,Java,源码中声明与常量的信息。包含类,/,继承的超类,/,实现的接口的声明信息、域与方法声明信息和常量池,3.,方法信息。对应,Java,源码中语句和表达式对应的信息。包含字节码、异常处理器表、求值栈与局部变量区大小、求值栈的类型记录、调试符号信息,Java,源码编译由以下三个过程组成:,1.,分析和输入到符号表,2.,注解处理,3.,语义分析和生成,class,文件,类执行机制,JVM,是基于栈的体系结构来执行,class,字节码的。线程创建后,都会产生程序计数器(,PC,)和栈(,Stack,),程序计数器存放下一条要执行的指令在方法内的偏移量,栈中存放一个个栈帧,每个栈帧对应着每个方法的每次调用,而栈帧又是有局部变量区和操作数栈两部分组成,局部变量区用于存放方法中的局部变量和参数,操作数栈中用于存放方法执行过程中产生的中间结果。栈的结构如下图所示:,Size of Java Types&Objects,From Java Spec-,byte:8-bit,short:16-bit,Int:32-bit,long:64-bit,char:16 bit unsigned integer,float: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:16 bytes,Character:16 bytes,Float:16 bytes,Double:16 bytes,Boolean:16 bytes,So,how about Object?,new Object(),new,new Object()=8 bytes,new Double itself=16 bytes,new ArrayList itself=24 bytes,Why?,Memory Layout of Boolean,HEADER:8 bytes 8 value:1 byte 9padding:7 bytes 16,Shallow and 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 amount 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 referenced 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 Reference,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.hprof.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 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 managed by OS,New/Young,what is interned String?,How GC works?GC of Serial Collection,Empty,Empty,Survivor Spaces,Old,Eden,From,To,New Object,How GC works?Full GC of Serial Collection,Full GC:A old or permanent generation collection(Stop the world),Before Full GC,After Full GC,GC Algorithm,Serial Collector,as we described in previous slides,default collector for client-class server,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 collector,Eventually,this collector 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 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 garbage 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 parallel compacting collector,-XX:MaxGCPauseMillis=n,-XX:GCTimeRatio=n 1/(1+n),Diagnosing a Garbage Collection problem,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,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 byte,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:MaxPermSize,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 used/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(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 provided 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 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 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 ulimit)?,#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-verbose: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 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,
展开阅读全文

开通  VIP、SVIP  下载更划算
下载10份以上建议开通 VIP 会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        关注我们

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服