3. We’ll learn gc can be used in jdk 1.6 how to use these gc what difference between these gc when gc will be executed how to solve oom how to monitor gc how to tuning gc how to realiazegc
5. Basic Concept—runtime data area -Xss PCregister local method stack local var -XX:PermSize –XX:MaxPermSize oper stack method area stack frame heap JVMmethod stack -Xms -Xmx
6. Basic concept—memory allocation 1、mostly from heap such as Object o=new Object(); 2、from stack native type variable 3、outside heap DirectByteBuffer or use Unsafe.allocateMemory or some jdk class such as Deflater/Inflater or jni
7. Basic concept—garbage collection GC collect memory assumed by “dead” objects. 1. Hotspot think no reference object is a “dead” object 2. Hotspot has four reference types Strong、Soft、Weak、Phantom
8. Basic concept—generations A research report provided by IBM shows about 80% -- 95% objects are temp objects,so Hotspot split the heap to two generations.
9. Basic concept—two generations -Xmn New Generation Eden S0 S1 Old Generation -XX:SurvivorRatio note 1:There are mostly temp objects in new generation,so hotspot use copy Algorithm to collect,then split new gen to eden and two equal survivor space. note 2: The collection for new gen usally called minor gc; the collection for old gen usally called major gc or full gc (because commonly it need to scan new/old gen and perm gen, except CMS).
10. some tips: 1、when system running,we can use jstat –gcutilto monitor the memory changes in parts(such as s0/s1/eden/old); 2、add -XX:+PrintGCDetails –Xloggc:<file> to startup args,then we can see gc status in this file; 3、jmap –heap or jstat –gc to see parts memory capacity & consumption.
12. Minor GC Serial Parallel Scavenge ParNew Major GC Serial MSC Parallel MSC CMS Parallel Compacting
13. We’ll learn gc can be used in jdk 1.6 how to use these gc what difference between these gc when gc will be executed how to solve oom how to monitor gc how to tuning gc how to realiazegc √
14. Minor GC Serial Parallel Scavenge ParNew which one should I choose?
15. Minor GC—Serial 1. client class machine default,also can use -XX:+UseSerialGC; 2. eden/s0/s1capacity use -XX:SurvivorRatioto control, default value is 8,meaning is eden:s0.
16. Minor GC—Serial mostly allocate on TLABor eden,only two situations below will allocate on old: 1、need allocate space > eden space; 2、when add PretenureSizeThreshold,need allocate space > PretenureSizeThreshold. public class SerialGCDemo{ public static void main(String[] args) throws Exception{ byte[] bytes=new byte[1024*1024*2]; byte[] bytes2=new byte[1024*1024*2]; byte[] bytes3=new byte[1024*1024*2]; Thread.sleep(3000); byte[] bytes4=new byte[1024*1024*4]; Thread.sleep(3000); } } -Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC -Xms20M –Xmx20M –Xmn10M -XX:PretenureSizeThreshold=3145728 –XX:+UseSerialGC
17. Minor GC—Serial gc occurs when eden space is not enough. public class SerialGCDemo{ public static void main(String[] args) throws Exception{ byte[] bytes=new byte[1024*1024*2]; byte[] bytes2=new byte[1024*1024*2]; byte[] bytes3=new byte[1024*1024*2]; System.out.println(“step 1"); byte[] bytes4=new byte[1024*1024*2]; Thread.sleep(3000); System.out.println(“step 2"); byte[] bytes5=new byte[1024*1024*2]; byte[] bytes6=new byte[1024*1024*2]; System.out.println(“step 3"); byte[] bytes7=new byte[1024*1024*2]; Thread.sleep(3000); } } -Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC
18. Minor GC—Serial The example executes cause one minor gc and one full gc,because this rule: before gc,serialgc will test if average space when minor gc from eden promotion to old > old gen free space, if true,thenfull,iffalse,then base on HandleProtomotionFailure value.
20. Minor GC—Serial The example execute different when use two type args,because this rule: when minor gc occurs: average space when minor gc from eden promotion to old < old gen free space < eden+from capacity if HandlePromotionFailure=true,then minor gcoccurs,iffalse,then full gc occurs.
21. Minor GC—Serial The rule for new gen promotion to old gen. 1. the object still alive after some times minor gc; based on MaxTenuringThreshold,default value is 15. 2. to space is not enough,then direct to old.
23. Minor GC—Serial change the object1 code: SerialGCMemoryObject object1=new SerialGCMemoryObject(2); -Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC
24. Minor GC—Serial The example shows object1 direct to oldwhen the second minor gc occurs, because this rule: after minor gc, then TenuringThreshold will be recaculted. (the first time TenuringThreshold= MaxTenuringThreshold) caculate rule: sum the bytes in per age,when the sum value > to space half,minor(age, MaxTenuringThreshold) can use PrintTenuringDistribution to see TenuringThreshold: Desired survivor size 524288 bytes, new threshold 1 (max 15). so MaxTenuringThreshold only represents max tenuringthreshold.
26. Minor GC—ParNew when use cmsgc,this is default,also can use -XX:+UseParNewGC to use; 2. eden/s0/s1 capacity controlled by -XX:SurvivorRatio,the default value is 8,meaning eden:s0. ParNew memory allocation and gc is same as Serial,only difference is ParNew use multi threads to gc,but if add -XX:+UseAdaptiveSizePolicy,then many differences.
28. Minor GC—PS server class machine default,also can add -XX:+UseParallelGC to use; eden/s0/s1 capacity can be controlled by InitialSurvivorRatioor SurvivorRatio,when no above two args,then use InitialSurvivorRatio,the default value is 8,but its meaning isnew gen:survivor.
29. Minor GC—PS mostly allocate on TLAB or eden. public class PSGCDemo{ public static void main(String[] args) throws Exception{ byte[] bytes=new byte[1024*1024*2]; byte[] bytes2=new byte[1024*1024*2]; byte[] bytes3=new byte[1024*1024*2]; Thread.sleep(3000); byte[] bytes4=new byte[1024*1024*4]; Thread.sleep(3000); } } -Xms20M –Xmx20M –Xmn10M –XX:SurvivorRatio=8 –XX:+UseParallelGC
30. Minor GC—PS The example shows bytes4 object allocate on old gen, because this rule: when allocate fail on TLAB、eden,test if allocate space>= half of eden space,iftrue,then allocate on old gen.
31. Minor GC—PS when eden space is not enough,and not allocate on old,thengc occurs. public class PSGCDemo{ public static void main(String[] args) throws Exception{ byte[] bytes=new byte[1024*1024*2]; byte[] bytes2=new byte[1024*1024*2]; byte[] bytes3=new byte[1024*1024*2]; System.out.println(“step 1"); byte[] bytes4=new byte[1024*1024*2]; Thread.sleep(3000); System.out.println(“step 2"); byte[] bytes5=new byte[1024*1024*2]; byte[] bytes6=new byte[1024*1024*2]; System.out.println(“step 3"); byte[] bytes7=new byte[1024*1024*2]; Thread.sleep(3000); } } -Xms20M –Xmx20M –Xmn10M –XX:SurvivorRatio=8 –XX:+UseParallelGC -XX:+PrintGCDetails –XX:verbose:gc
32. Minor GC—PS the example shows one minor gc and two full gc,because this rule: 1. before gc,serialgc will test if average space when minor gcfrom eden promotion to old > old gen free space, if true,thenfull; 2. after gc,also do above test.
33. Minor GC—PS The rule for new gen promotion to old gen. 1. the object still alive after some times minor gc: AlwaysTenure, the default value is false,meaning the alive objects will be promoted to old when minor gc. NeverTenure, the default value isfalse,meaning the alive objectswill never be promoted to old,except the to space is not enough. if the above two args are false and UseAdaptiveSizePolicy,then the first time based onInitialTenuringThreshold,but will be adjusted after every minor gc,if not UseAdaptiveSizePolicy,then based onMaxTenuringThreshold. 2. to space is not enough,then direct to old gen.
34. Minor GC—PS After minor gc,ifUseAdaptiveSizePolicy,it’ll adjust eden space and to space based on runtime data,if don’t want to be adjusted,then add -XX:-UseAdaptiveSizePolicyto startup args.
36. Major GC Serial MSC Parallel MSC CMS Parallel Compacting which one should I choose?
37. Major GC—Serial client class machine default,also can add -XX:+UseSerialGC to startup args to use.
38. Major GC—Serial when occurs 1. old genis not enough; 2. perm gen is not enough; 3. the pessimistic rule when minor gc; 4. heap dump; 5. the code call System.gc,can use -XX:+DisableExplicitGC to disable.
40. Major GC—Parallel MSC 1. server class machine default,also can add -XX:+UseParallelGC to use; 2. parallel threads cpu core<=8 ? cpu core : 3+(cpu core*5)/8 or use -XX:ParallelGCThreads=x to set.
41. Major GC—Parallel MSC occur rule is same as Serial. note: if ScavengeBeforeFullGCis true(the default value), then execute minor GCfirst.
43. Major GC—Parallel Compacting 1. can add -XX:+UseParallelOldGCto use; 2. parallel threads: cpu core<=8 ? cpu core : 3+(cpu core*5)/8 or use -XX:ParallelGCThreads=xto set.
46. Major GC—CMS 1. can add -XX:+UseConcMarkSweepGC to use; 2. mostly concurrent; 3. concurrent threads: (parallel gc threads +3)/4 or use–XX:ParallelCMSThreads=X to set.
47. Major GC—CMS occur rule 1. when old gen used reach a percentage; default is 92%,can add PrintCMSInitiationStatistics(cann’t use in 1.5)to see default value,the default value caculate rule: ((100 - MinHeapFreeRatio) +(double)(CMSTriggerRatio * MinHeapFreeRatio) / 100.0)/ 100.0;MinHeapFreeRatiodefault value: 40 CMSTriggerRatiodefault value: 80 or use CMSInitiatingOccupancyFraction to set; 2. when perm gen used reach a percentage; perm gen use cms need to add: -XX:+CMSClassUnloadingEnabled default is 92%,default value caculate rule: ((100 - MinHeapFreeRatio) +(double)(CMSTriggerPermRatio* MinHeapFreeRatio) / 100.0)/ 100.0; MinHeapFreeRatiodefault value:40 CMSTriggerPermRatiodefault value: 80 or use CMSInitiatingPermOccupancyFractionto set;
48. Major GC—CMS occur rule 3. Hotspot decide when to executeCMS GCbased on runtime data; or to use -XX:+UseCMSInitiatingOccupancyOnly to disable this rule; 4. the code call System.gc when set ExplicitGCInvokesConcurrentto true; note: when use this and NIO,maybe occur the bug.
50. Major GC—CMS 1. Promotion Failed when minor gc,to spaceis not enough,then promotion to old,but old is not enough too,then promotion failed;solution: increase to space,increase old space, or decrease cmsgc occur percentage. 2. Concurrent mode failure when need allocate on old,butcmsgc is running,then concurrent mode failure,and to keep safe,hotspot will execute serial full gc;solution:increase old space, or decrease cmsgc occur percentage.
56. We’ll learn gc can be used in jdk 1.6 how to use these gc what difference between these gc when gc will be executed how to solve oom how to monitor gc how to tuning gc how to realiazegc √ √ question: how to let minor gc use psgc,and major gc use parallel compacting gc? √ question:what is difference between psgcand parnewgc? √ question: pls write a code,when it execute first occur 5 minor gc,and 2 full gc,and 2 minor gc,finally 1 full gc.
59. OOMtype the above examples show: 1、java.lang.OutOfMemoryError: GC overhead limit exceeded 2、java.lang.OutOfMemoryError: Java heap space 3、java.lang.OutOfMemoryError: unable to create new native thread and also belows: 4、java.lang.OutOfMemoryError: request bytes for . Out of swap space? two reason: memory is not enough; address space exhausted(for example on 32 bit linux the process can only use 3G address space) this OOMwill cause Java process exit; monitor who use native memory: google-perftools 5、java.lang.OutOfMemoryError: PermGen space PermGenis not enough;
60. OOMsolution Java Heap SpaceOOM: 1. add -XX:+HeapDumpOnOutOfMemoryError; 2. jmap–histo; 3. jmap –dump; 4. hand to solve; 5. heap too small? Out of swap: 1. heap too large? 2. Google perftools,if not help,then no way... unable to create new native thread: 1. too many threads; 2. –Xss; PermGen space: 1. PermGentoo small? 2. too many ClassLoaders?
62. GCMonitor 1. jstat–gcutil [pid] [intervel] [count] 2. -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -Xloggc:[file] gclog can be analised by GCLogVieweror gchisto. 3. if support GUI,then can use visualvm.
63. who consume the memory 1. alive long situation,easy,jmap –dump,then MAT; 2. alive short,if no GUI,thenin trouble,ifGUI,thenjprofiler/visualvm.
64. We’ll learn gc can be used in jdk 1.6 how to use these gc what difference between these gc when gc will be executed how to solve oom how to monitor gc how to tuning gc how to realiazegc √ √ √ √ √ java.lang.OutOfMemoryError: Java Heap Space,what can we do? √ pls tell me how many minor gc and full gc the app executes.
68. tuning case Goal decrease full gc frequency and pause time caused by gc. Method decrease response time or requests,difficult; decrease memory used per request,difficult; decrease memory promoted to old every minor gc,it’s ok;
69. tuning case decrease memory promoted to old every minor gc increase new gen space,now new gen space is big,soit does not work; increase survivor space,it maybe works.
70. tuning case increase Survivor space now PS GC,so survivor space is adjusted,but monitor this case,we can see mostly survivor space only have 1—4MB,it’s too small; so add -XX:-UseAdaptiveSizePolicy; caculate survivor spaceneed size see current to space size,and after minor gc,old gen increased size,then to space+old gen increased size will be survivor space need size; sum many times then average; adjust survivor space to desired size; after the adjustion,minorgcmore,but full gc decrease to every two hours.
71. tuning case the method decrease full gcfrequency,but the pause time caused by gc only decrease 10%; keep Survivorspace,and use cms,the final startup args: -Xms1536m -Xmx1536m -Xmn700m -XX:SurvivorRatio=7 -XX:+UseConcMarkSweepGC-XX:CMSMaxAbortablePrecleanTime=1000 -XX:+CMSClassUnloadingEnabled-XX:+DisableExplicitGC
73. GC Tuning—common policy decrease full gc frequency–common problem oldgen used too high;common reason: cache too many objects;for example: preparedstatement cacheset too big when use oracle driver;solution: dump then mat,bingo!
74. GC Tuning—common policy decrease full gc frequency–common problem many objects promotion to old gen;common reason: survivor spacetoo small; response time is too slow; many memory used by per request;solution: decrease minor gc frequency; let object collect when minor gc,such as increase suvivorspace,new gen; use cmsgc;tuning application;
75. GC Tuning—common policy decrease full gc time common reason:old gen is too big;solution: parallel gc?decrease heap or old gen; add cpu or upgrade cpu.
76. GC Tuning—common policy decrease minor gc frequency common reasonminor gc too small; many memory used by per request;solutionincrease heap,new or eden; decrease memory used by per request.
77. GC Tuning—common policy decrease minor gc time common reason response time too slowwsolutionadd cpu or upgrade cpu; if u use cmsgc,then as much as possible avoid object allocate or promote to old gen,or change to psgc.
78. GC Tuning GC Tuning is art! The final solution is decrease response time or memory used per request; or upgrade to 64 bit,then use large heap,of course it needs more cpu.
79. We’ll learn gc can be used in jdk 1.6 how to use these gc what difference between these gc when gc will be executed how to solve oom how to monitor gc how to tuning gc how to realiazegc √ √ √ √ √ √ √
80. Garbage Collection 1. usually use below two methods:1.1 Reference counter not suitable for complex object reference situations; count will decrease performance; good point is when counters decrease to zero,then collect. 1.2 Tracing hotspot use; need pause the application. common algithorm: Copying/Mark-Sweep/Mark-Compact
81. Garbage Collection Hotspot scan objects from root set; root set 1. current running thread; 2. public or static variables; 3. JVM Handles; 4. JNIHandles;
82. Garbage Collection how to pause thread?safepointfirst: test a memory page readable; keypoint:only the code will cause the reference change need pause; so when gcoccurs,it’ll submit a request to core,then core set the memory page cannot read,when code execute reach safepoint,then pause.
84. Minor GC allocate on a continous space and use bump the pointer. A B C D
85. Minor GC the policy: scan new gen objects, find alive objects; use copying algorithm to finish collection.
86. Minor GC because minor gc only scan new gen,if old gen references new gen,how to do?when assign a reference value,will pass a write barrier;test if old gen ref new gen object,iftrue,then record to remember set;when minor gc,the objects in remember set also put into root set.
89. Major GC—Serial Garbage Collection Policy based on Mark Sweep Compact algorithm. split four phases: 1. mark which object alive; 2. caculate new address; 3. update pointer to new address; 4. move object to new address.
94. Major GC—CMS Memory Allocation Policy 1. first get freelist lock; 2. find which chunk can put the object; 3. if now gc in marking phase,the mark the object alive.
95. Major GC—CMS start a cms thread first; based on Mark-Sweep,split into five phase; Initial Marking(Stop-the-world)Concurrent MarkingPreClean(Sun jdk 1.5 added)Final Marking(Stop-the-world) Concurrent Sweeping
96. Major GC—CMS Initial Marking (Stop-the-world)markroot setdirect ref objects; Concurrent Markingconcurrent mark phase 1 marked objects recursively; Mod Union Tableto solve the ref change when minor gc executes; Card Table to solve the ref change in the old gen.
97. Major GC—CMS Preclean scan again phase 2 marked objects and dirty objects;-XX: CMSScheduleRemarkEdenSizeThreshold、-XX: CMSScheduleRemarkEdenPenetration this step maybe causes bug,when object need allocate old, but remark phase not execute,then old space not enough, so current step have a timeout arg,default value is 5000ms, or use -XX: CMSMaxAbortablePrecleanTime to control.
98. Major GC—CMS Final Marking (Stop-the-world)scan Mod Union Table/Card Table,then remark. Concurrent Sweepingcollect the memory used by “dead” objects.
99. Major GC—CMS good point and weak point; mostly concurrent,so it’ll only pause application little time; float garbage,so need more memory size; memory fragment; fight for cpu,so it not suit for cpu intensive app; multi mark,sogc time is longer then parallel gc; memory allocation need use free list,soit’s slower then bumppointer allocate; concurrent with application,so free list will compete.
101. Study SUN JDK V1.6 GC sources download JDK sources; use Source Insightetc. tools to see SerialGC、ParNewGC GenCollectedHeap::mem_allocate PS GC ParallelScavengeHeap::mem_allocate CMS GC ConcurrentMarkSweepThread::run CMSCollector::collect
102. We’ll learn gc can be used in jdk 1.6 how to use these gc what difference between these gc when gc will be executed how to solve oom how to monitor gc how to tuning gc how to realiazegc √ √ √ √ √ √ √ √
103. References 1. GC Tuning in the Hotspot 2. Our Collectors 3. CMS GC 4. why now 5. hotspot 6.0 gc tuning 6. memory management in hotspot whitepaper