Your SlideShare is downloading. ×
0
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Sun jdk 1.6 gc english version
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Sun jdk 1.6 gc english version

8,514

Published on

In this slide,I'll show the gc can be used in jdk 1.6,how to use,the difference,how to solve oom,how to tuning gc,and how to realize gc.

In this slide,I'll show the gc can be used in jdk 1.6,how to use,the difference,how to solve oom,how to tuning gc,and how to realize gc.

3 Comments
23 Likes
Statistics
Notes
No Downloads
Views
Total Views
8,514
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
448
Comments
3
Likes
23
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Sun JDK 1.6 GC(Garbage Collector) <br />bluedavy http://blog.bluedavy.com<br />2010-05-13 V0.2<br /> 2010-05-19 V0.5<br />2010-06-21 V0.8<br />2010-08-03 V0.9<br />
  • 2. Java:Auto Memory Management<br />whyneed learn GC?<br />OOM? <br />An unexpected long pause?<br />GC occur frequently?<br />
  • 3. We’ll learn<br />gc can be used in jdk 1.6<br />how to use these gc<br />what difference between these gc<br />when gc will be executed<br />how to solve oom<br />how to monitor gc<br />how to tuning gc<br />how to realiazegc<br />
  • 4. GC:Garbage Collector<br />not only for gc collection<br />also for memory allocate<br />
  • 5. Basic Concept—runtime data area<br />-Xss<br />PCregister<br />local method stack<br />local var<br />-XX:PermSize –XX:MaxPermSize<br />oper stack<br />method area<br />stack frame<br />heap<br />JVMmethod stack<br />-Xms -Xmx<br />
  • 6. Basic concept—memory allocation<br />1、mostly from heap<br />such as Object o=new Object(); <br />2、from stack<br />native type variable <br />3、outside heap<br />DirectByteBuffer<br />or use Unsafe.allocateMemory<br /> or some jdk class such as Deflater/Inflater<br /> or jni<br />
  • 7. Basic concept—garbage collection<br />GC collect memory assumed by “dead” objects.<br />1. Hotspot think no reference object is a “dead” object<br />2. Hotspot has four reference types<br />Strong、Soft、Weak、Phantom<br />
  • 8. Basic concept—generations<br />A research report provided by IBM shows about 80% -- 95%<br />objects are temp objects,so Hotspot split the heap to two <br />generations.<br />
  • 9. Basic concept—two generations<br />-Xmn<br />New Generation<br />Eden<br />S0<br />S1<br />Old Generation<br />-XX:SurvivorRatio<br />note 1:There are mostly temp objects in new generation,so hotspot use copy Algorithm to<br />collect,then split new gen to eden and two equal survivor space.<br />note 2: The collection for new gen usally called minor gc; the collection for old gen usally<br /> called major gc or full gc (because commonly it need to scan new/old gen and <br /> perm gen, except CMS).<br />
  • 10. some tips:<br />1、when system running,we can use jstat –gcutilto monitor the memory changes in parts(such as s0/s1/eden/old);<br />2、add -XX:+PrintGCDetails –Xloggc:&lt;file&gt; to startup args,then we can see gc status in this file;<br />3、jmap –heap or jstat –gc to see parts memory capacity &amp; consumption.<br />
  • 11. how many minor gc type?<br /> &amp; full gc type?<br />
  • 12. Minor GC<br />Serial<br />Parallel Scavenge<br />ParNew<br />Major GC<br />Serial MSC<br />Parallel MSC<br />CMS<br />Parallel Compacting<br />
  • 13. We’ll learn<br />gc can be used in jdk 1.6<br />how to use these gc<br />what difference between these gc<br />when gc will be executed<br />how to solve oom<br />how to monitor gc<br />how to tuning gc<br />how to realiazegc<br />√<br />
  • 14. Minor GC<br />Serial<br />Parallel Scavenge<br />ParNew<br />which one should I choose?<br />
  • 15. Minor GC—Serial<br />1. client class machine default,also can use -XX:+UseSerialGC;<br />2. eden/s0/s1capacity use -XX:SurvivorRatioto control,<br /> default value is 8,meaning is eden:s0.<br />
  • 16. Minor GC—Serial<br />mostly allocate on TLABor eden,only two situations below will <br />allocate on old:<br />1、need allocate space &gt; eden space;<br />2、when add PretenureSizeThreshold,need allocate space &gt; PretenureSizeThreshold.<br /> public class SerialGCDemo{<br /> public static void main(String[] args) throws Exception{<br /> byte[] bytes=new byte[1024*1024*2];<br /> byte[] bytes2=new byte[1024*1024*2];<br /> byte[] bytes3=new byte[1024*1024*2];<br />Thread.sleep(3000);<br /> byte[] bytes4=new byte[1024*1024*4];<br />Thread.sleep(3000);<br /> }<br />}<br />-Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC<br />-Xms20M –Xmx20M –Xmn10M -XX:PretenureSizeThreshold=3145728 –XX:+UseSerialGC<br />
  • 17. Minor GC—Serial<br />gc occurs when eden space is not enough.<br />public class SerialGCDemo{<br /> public static void main(String[] args) throws Exception{<br /> byte[] bytes=new byte[1024*1024*2];<br /> byte[] bytes2=new byte[1024*1024*2];<br /> byte[] bytes3=new byte[1024*1024*2];<br />System.out.println(“step 1&quot;);<br /> byte[] bytes4=new byte[1024*1024*2];<br />Thread.sleep(3000);<br />System.out.println(“step 2&quot;);<br /> byte[] bytes5=new byte[1024*1024*2];<br /> byte[] bytes6=new byte[1024*1024*2];<br />System.out.println(“step 3&quot;);<br /> byte[] bytes7=new byte[1024*1024*2];<br />Thread.sleep(3000); <br /> }<br />}<br />-Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC<br />
  • 18. Minor GC—Serial<br />The example executes cause one minor gc and one full gc,because<br />this rule:<br />before gc,serialgc will test if average space when minor gc from eden<br />promotion to old &gt; old gen free space, if true,thenfull,iffalse,then<br />base on HandleProtomotionFailure value.<br />
  • 19. Minor GC—Serial<br /> public class SerialGCDemo{<br /> public static void main(String[] args) throws Exception{<br /> byte[] bytes=new byte[1024*1024*2];<br /> byte[] bytes2=new byte[1024*1024*2];<br /> byte[] bytes3=new byte[1024*1024*2];<br />System.out.println(&quot;step 1&quot;);<br /> bytes=null;<br /> byte[] bytes4=new byte[1024*1024*2];<br />Thread.sleep(3000);<br />System.out.println(&quot;step 2&quot;);<br /> byte[] bytes5=new byte[1024*1024*2];<br /> byte[] bytes6=new byte[1024*1024*2];<br /> bytes4=null;<br /> bytes5=null;<br /> bytes6=null;<br />System.out.println(&quot;step 3&quot;);<br /> byte[] bytes7=new byte[1024*1024*2];<br />Thread.sleep(3000); <br /> }<br />}<br />-Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC<br />-Xms20M –Xmx20M –Xmn10M -XX:-HandlePromotionFailure –XX:+UseSerialGC<br />
  • 20. Minor GC—Serial<br />The example execute different when use two type args,because<br />this rule:<br />when minor gc occurs:<br />average space when minor gc from eden<br />promotion to old &lt; old gen free space &lt; eden+from capacity<br />if HandlePromotionFailure=true,then minor gcoccurs,iffalse,then full gc<br />occurs.<br />
  • 21. Minor GC—Serial<br />The rule for new gen promotion to old gen.<br />1. the object still alive after some times minor gc;<br /> based on MaxTenuringThreshold,default value is 15.<br />2. to space is not enough,then direct to old.<br />
  • 22. Minor GC—Serial<br />public class SerialGCThreshold{<br /> public static void main(String[] args) throws Exception{<br />SerialGCMemoryObject object1=new SerialGCMemoryObject(1);<br />SerialGCMemoryObject object2=new SerialGCMemoryObject(8);<br />SerialGCMemoryObject object3=new SerialGCMemoryObject(8);<br />SerialGCMemoryObject object4=new SerialGCMemoryObject(8);<br /> object2=null;<br /> object3=null;<br />SerialGCMemoryObject object5=new SerialGCMemoryObject(8);<br />Thread.sleep(4000);<br /> object2=new SerialGCMemoryObject(8);<br /> object3=new SerialGCMemoryObject(8);<br /> object2=null;<br /> object3=null;<br /> object5=null;<br />SerialGCMemoryObject object6=new SerialGCMemoryObject(8);<br />Thread.sleep(5000);<br /> }<br />}<br />class SerialGCMemoryObject{<br /> private byte[] bytes=null;<br /> public SerialGCMemoryObject(int multi){<br /> bytes=new byte[1024*256*multi];<br /> }<br />}<br />-Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC<br />-Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC<br />-XX:MaxTenuringThreshold=1<br />
  • 23. Minor GC—Serial<br />change the object1 code:<br />SerialGCMemoryObject object1=new SerialGCMemoryObject(2);<br />-Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC<br />
  • 24. Minor GC—Serial<br />The example shows object1 direct to oldwhen the second minor gc<br />occurs, because this rule:<br />after minor gc, then TenuringThreshold will be recaculted.<br />(the first time TenuringThreshold= MaxTenuringThreshold)<br />caculate rule:<br />sum the bytes in per age,when the sum value &gt; to space half,minor(age,<br />MaxTenuringThreshold)<br />can use PrintTenuringDistribution to see TenuringThreshold:<br />Desired survivor size 524288 bytes, new threshold 1 (max 15).<br />so MaxTenuringThreshold only represents max tenuringthreshold.<br />
  • 25. Minor GC—Serial<br /> [GC [DefNew: 11509K-&gt;1138K(14336K), 0.0110060 secs] 11509K-&gt;1138K(38912K), <br /> 0.0112610 secs] [Times: user=0.00 sys=0.01, real=0.01 secs]<br />
  • 26. Minor GC—ParNew<br />when use cmsgc,this is default,also can use <br />-XX:+UseParNewGC to use;<br />2. eden/s0/s1 capacity controlled by -XX:SurvivorRatio,the default<br /> value is 8,meaning eden:s0.<br />ParNew memory allocation and gc is same as Serial,only difference<br />is ParNew use multi threads to gc,but if add <br />-XX:+UseAdaptiveSizePolicy,then many differences.<br />
  • 27. Minor GC—ParNew<br /> [GC [ParNew: 11509K-&gt;1152K(14336K), 0.0129150 secs] 11509K-&gt;1152K(38912K), <br /> 0.0131890 secs] [Times: user=0.05 sys=0.02, real=0.02 secs]<br />if add -XX:+UseAdaptiveSizePolicy to startup args,then output:<br /> [GC [ASParNew: 7495K-&gt;120K(9216K), 0.0403410 secs] 7495K-&gt;7294K(19456K), 0.0406480 secs] [Times: user=0.06 sys=0.15, real=0.04 secs]<br />
  • 28. Minor GC—PS<br />server class machine default,also can add -XX:+UseParallelGC to use;<br />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.<br />
  • 29. Minor GC—PS<br />mostly allocate on TLAB or eden.<br /> public class PSGCDemo{<br /> public static void main(String[] args) throws Exception{<br /> byte[] bytes=new byte[1024*1024*2];<br /> byte[] bytes2=new byte[1024*1024*2];<br /> byte[] bytes3=new byte[1024*1024*2];<br />Thread.sleep(3000);<br /> byte[] bytes4=new byte[1024*1024*4];<br />Thread.sleep(3000);<br /> }<br />}<br />-Xms20M –Xmx20M –Xmn10M –XX:SurvivorRatio=8 –XX:+UseParallelGC<br />
  • 30. Minor GC—PS<br />The example shows bytes4 object allocate on old gen, because<br />this rule:<br />when allocate fail on TLAB、eden,test if allocate space&gt;= half of eden<br />space,iftrue,then allocate on old gen.<br />
  • 31. Minor GC—PS<br />when eden space is not enough,and not allocate on old,thengc occurs.<br />public class PSGCDemo{<br /> public static void main(String[] args) throws Exception{<br /> byte[] bytes=new byte[1024*1024*2];<br /> byte[] bytes2=new byte[1024*1024*2];<br /> byte[] bytes3=new byte[1024*1024*2];<br />System.out.println(“step 1&quot;);<br /> byte[] bytes4=new byte[1024*1024*2];<br />Thread.sleep(3000);<br />System.out.println(“step 2&quot;);<br /> byte[] bytes5=new byte[1024*1024*2];<br /> byte[] bytes6=new byte[1024*1024*2];<br />System.out.println(“step 3&quot;);<br /> byte[] bytes7=new byte[1024*1024*2];<br />Thread.sleep(3000); <br /> }<br />}<br />-Xms20M –Xmx20M –Xmn10M –XX:SurvivorRatio=8 –XX:+UseParallelGC<br />-XX:+PrintGCDetails –XX:verbose:gc<br />
  • 32. Minor GC—PS<br />the example shows one minor gc and two full gc,because this rule:<br />1. before gc,serialgc will test if average space when minor gcfrom<br />eden promotion to old &gt; old gen free space, if true,thenfull;<br />2. after gc,also do above test.<br />
  • 33. Minor GC—PS<br />The rule for new gen promotion to old gen.<br />1. the object still alive after some times minor gc:<br />AlwaysTenure, the default value is false,meaning the alive objects will be promoted to old when minor gc.<br />NeverTenure, the default value isfalse,meaning the alive objectswill never be promoted to old,except the to space is not enough.<br />if the above two args are false and UseAdaptiveSizePolicy,then the first time based onInitialTenuringThreshold,but will be adjusted after<br /> every minor gc,if not UseAdaptiveSizePolicy,then based onMaxTenuringThreshold.<br />2. to space is not enough,then direct to old gen.<br />
  • 34. Minor GC—PS<br />After minor gc,ifUseAdaptiveSizePolicy,it’ll adjust eden space and to <br />space based on runtime data,if don’t want to be adjusted,then add<br />-XX:-UseAdaptiveSizePolicyto startup args.<br />
  • 35. Minor GC—PS<br />[GC [PSYoungGen: 11509K-&gt;1184K(14336K)] 11509K-&gt;1184K(38912K), 0.0113360 secs]<br /> [Times: user=0.03 sys=0.01, real=0.01 secs]<br />
  • 36. Major GC<br />Serial MSC<br />Parallel MSC<br />CMS<br />Parallel Compacting<br />which one should I choose?<br />
  • 37. Major GC—Serial<br />client class machine default,also can add -XX:+UseSerialGC<br />to startup args to use.<br />
  • 38. Major GC—Serial<br />when occurs<br />1. old genis not enough;<br />2. perm gen is not enough;<br />3. the pessimistic rule when minor gc;<br />4. heap dump;<br />5. the code call System.gc,can use -XX:+DisableExplicitGC to <br /> disable.<br />
  • 39. Major GC—Serial<br />[Full GC [Tenured: 9216K-&gt;4210K(10240K), 0.0066570 secs] 16584K-&gt;4210K(19456K), [Perm : 1692K-&gt;1692K(16384K)], 0.0067070 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]<br />
  • 40. Major GC—Parallel MSC<br />1. server class machine default,also can add -XX:+UseParallelGC<br /> to use;<br />2. parallel threads<br />cpu core&lt;=8 ? cpu core : 3+(cpu core*5)/8<br />or use -XX:ParallelGCThreads=x to set.<br />
  • 41. Major GC—Parallel MSC<br />occur rule is same as Serial.<br />note: if ScavengeBeforeFullGCis true(the default value),<br /> then execute minor GCfirst.<br />
  • 42. Major GC—Parallel MSC<br />[Full GC [PSYoungGen: 1208K-&gt;0K(8960K)] [PSOldGen: 6144K-&gt;7282K(10240K)] 7352K-&gt;7282K(19200K) [PSPermGen: 1686K-&gt;1686K(16384K)], 0.0165880 secs] [Times: user=0.01 sys=0.01, real=0.02 secs] <br />
  • 43. Major GC—Parallel Compacting<br />1. can add -XX:+UseParallelOldGCto use;<br />2. parallel threads:<br />cpu core&lt;=8 ? cpu core : 3+(cpu core*5)/8<br />or use -XX:ParallelGCThreads=xto set.<br />
  • 44. Major GC—Parallel Compacting<br />occur rule is same as Parallel MSC.<br />
  • 45. Major GC—Parallel Compacting<br />[Full GC [PSYoungGen: 1224K-&gt;0K(8960K)] [ParOldGen: 6144K-&gt;7282K(10240K)] 7368K-&gt;7282K(19200K) [PSPermGen: 1686K-&gt;1685K(16384K)], 0.0223510 secs] [Times: user=0.02 sys=0.06, real=0.03 secs]<br />
  • 46. Major GC—CMS<br />1. can add -XX:+UseConcMarkSweepGC to use;<br />2. mostly concurrent;<br />3. concurrent threads:<br /> (parallel gc threads +3)/4<br /> or use–XX:ParallelCMSThreads=X to set.<br />
  • 47. Major GC—CMS<br />occur rule<br />1. when old gen used reach a percentage;<br />default is 92%,can add PrintCMSInitiationStatistics(cann’t use in 1.5)to see default value,the default value caculate rule:<br /> ((100 - MinHeapFreeRatio) +(double)(CMSTriggerRatio * MinHeapFreeRatio) / 100.0)/ 100.0;MinHeapFreeRatiodefault value: 40 CMSTriggerRatiodefault value: 80<br />or use CMSInitiatingOccupancyFraction to set;<br />2. when perm gen used reach a percentage;<br /> perm gen use cms need to add: -XX:+CMSClassUnloadingEnabled<br />default is 92%,default value caculate rule:<br />((100 - MinHeapFreeRatio) +(double)(CMSTriggerPermRatio* MinHeapFreeRatio) / 100.0)/ 100.0;<br />MinHeapFreeRatiodefault value:40 CMSTriggerPermRatiodefault value: 80<br /> or use CMSInitiatingPermOccupancyFractionto set;<br />
  • 48. Major GC—CMS<br />occur rule<br />3. Hotspot decide when to executeCMS GCbased on runtime data;<br />or to use -XX:+UseCMSInitiatingOccupancyOnly to disable this rule;<br />4. the code call System.gc when set ExplicitGCInvokesConcurrentto <br /> true;<br /> note: when use this and NIO,maybe occur the bug.<br />
  • 49. Major GC—CMS<br />public class CMSGCOccur{<br /> public static void main(String[] args) throws Exception{<br /> byte[] bytes=new byte[1024*1024*2];<br /> byte[] bytes1=new byte[1024*1024*2];<br /> byte[] bytes2=new byte[1024*1024*2];<br /> byte[] bytes3=new byte[1024*1024*1];<br /> byte[] bytes4=new byte[1024*1024*2];<br />Thread.sleep(5000);<br /> }<br />}<br />-Xms20M –Xmx20M –Xmn10M -XX:+UseConcMarkSweepGC -XX:+UseCMSInitiatingOccupancyOnly -XX:+PrintGCDetails<br />-Xms20M –Xmx20M –Xmn10M -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails<br />
  • 50. Major GC—CMS<br />1. Promotion Failed<br />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. <br />2. Concurrent mode failure<br />when need allocate on old,butcmsgc is running,then<br /> concurrent mode failure,and to keep safe,hotspot will<br /> execute serial full gc;solution:increase old space,<br /> or decrease cmsgc occur percentage. <br />
  • 51. Major GC—CMS<br />[GC [1 CMS-initial-mark: 13433K(20480K)] 14465K(29696K), 0.0001830 secs] <br />[Times: user=0.00 sys=0.00, real=0.00 secs]<br />[CMS-concurrent-mark: 0.004/0.004 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] <br />[CMS-concurrent-preclean: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] <br />CMS: abort preclean due to time [CMS-concurrent-abortable-preclean: 0.007/5.042 secs] <br />[Times: user=0.00 sys=0.00, real=5.04 secs] <br />[GC[YG occupancy: 3300 K (9216 K)][Rescan (parallel) , 0.0002740 secs]<br />[weak refs processing, 0.0000090 secs] <br />[1 CMS-remark: 13433K(20480K)] 16734K(29696K), 0.0003710 secs] <br />[Times: user=0.00 sys=0.00, real=0.00 secs] <br />[CMS-concurrent-sweep: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] <br />[CMS-concurrent-reset: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]<br />when add -XX:+UseAdaptiveSizePolicy,then output CMS will change to ASCMS<br />CMS GC Log<br />
  • 52. GC—Default<br />
  • 53. GC—Collocation<br />
  • 54.
  • 55. JDKGC—Beautiful Future<br />Garbage First<br />
  • 56. We’ll learn<br />gc can be used in jdk 1.6<br />how to use these gc<br />what difference between these gc<br />when gc will be executed<br />how to solve oom<br />how to monitor gc<br />how to tuning gc<br />how to realiazegc<br />√<br />√<br />question: how to let minor gc use psgc,and major gc use parallel compacting gc?<br />√<br />question:what is difference between psgcand parnewgc?<br />√<br />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.<br />
  • 57. Let’s solve some OOM Case<br />
  • 58. OOM Cases<br />1、java -Xms20m -Xmx20m -Xmn10m -XX:+UseParallelGC<br /> com. bluedavy.oom.JavaHeapSpaceCase1<br />2、java -Xms20m -Xmx20m -Xmn10m <br /> -XX:+HeapDumpOnOutOfMemoryError<br /> com.bluedavy.oom.JavaHeapSpaceCase2<br />3、execute com.bluedavy.oom.JavaHeapSpaceCase3<br />4、 execute com.bluedavy.oom.JavaHeapSpaceCase4<br />5、java -Xms1536m -Xmx1536m -Xss100m com.bluedavy.oom.CannotCreateThreadCase<br />
  • 59. OOMtype<br />the above examples show:<br />1、java.lang.OutOfMemoryError: GC overhead limit exceeded<br />2、java.lang.OutOfMemoryError: Java heap space<br />3、java.lang.OutOfMemoryError: unable to create new native thread<br />and also belows:<br />4、java.lang.OutOfMemoryError: request bytes for . Out of swap space?<br />two reason: memory is not enough; address space exhausted(for <br /> example on 32 bit linux the process can only use 3G address space)<br />this OOMwill cause Java process exit;<br />monitor who use native memory: google-perftools<br />5、java.lang.OutOfMemoryError: PermGen space<br />PermGenis not enough;<br />
  • 60. OOMsolution<br />Java Heap SpaceOOM:<br />1. add -XX:+HeapDumpOnOutOfMemoryError;<br />2. jmap–histo;<br />3. jmap –dump;<br />4. hand to solve;<br />5. heap too small?<br />Out of swap:<br />1. heap too large?<br />2. Google perftools,if not help,then no way...<br />unable to create new native thread:<br />1. too many threads;<br />2. –Xss;<br />PermGen space:<br />1. PermGentoo small?<br />2. too many ClassLoaders?<br />
  • 61. OOM Reference<br />1. Sun JDK 6 troubleshooting Guide;<br />2.OOM<br />
  • 62. GCMonitor<br />1. jstat–gcutil [pid] [intervel] [count]<br />2. -verbose:gc<br /> -XX:+PrintGCDetails<br /> -XX:+PrintGCApplicationStoppedTime<br /> -XX:+PrintGCDateStamps<br /> -XX:+PrintHeapAtGC<br /> -Xloggc:[file]<br />gclog can be analised by GCLogVieweror gchisto.<br />3. if support GUI,then can use visualvm.<br />
  • 63. who consume the memory<br />1. alive long situation,easy,jmap –dump,then MAT;<br />2. alive short,if no GUI,thenin trouble,ifGUI,thenjprofiler/visualvm.<br />
  • 64. We’ll learn<br />gc can be used in jdk 1.6<br />how to use these gc<br />what difference between these gc<br />when gc will be executed<br />how to solve oom<br />how to monitor gc<br />how to tuning gc<br />how to realiazegc<br />√<br />√<br />√<br />√<br />√<br />java.lang.OutOfMemoryError: Java Heap Space,what can we do?<br />√<br />pls tell me how many minor gc and full gc the app executes.<br />
  • 65. tuning case<br />4 cpu,os: linux 2.6.18 32 bit<br />startup args<br />-server -Xms1536m -Xmx1536m –Xmn700m<br />gc log:<br />67919.817: [GC [PSYoungGen: 588706K-&gt;70592K(616832K)] 1408209K-&gt;906379K(1472896K), 0.0197090 secs] [Times: user=0.06 sys=0.00, real=0.02 secs]<br />67919.837: [Full GC [PSYoungGen: 70592K-&gt;0K(616832K)] [PSOldGen: 835787K-&gt;375316K(856064K)] 906379K-&gt;375316K(1472896K) [PSPermGen: 64826K-&gt;64826K(98304K)], 0.5478600 secs] [Times: user=0.55 sys=0.00, real=0.55 secs]<br />
  • 66. tuning case<br />minor gc info<br />
  • 67. tuning case<br />gc log:<br />68132.862: [GC [PSYoungGen: 594736K-&gt;63715K(609920K)] 1401225K-&gt;891090K(1465984K), 0.0309810 secs] [Times: user=0.06 sys=0.01, real=0.04 secs] <br />68132.893: [Full GC [PSYoungGen: 63715K-&gt;0K(609920K)] [PSOldGen: 827375K-&gt;368026K(856064K)] 891090K-&gt;368026K(1465984K) [PSPermGen: 64869K-&gt;64690K(98304K)], 0.5341070 secs] [Times: user=0.53 sys=0.00, real=0.53 secs]<br />then repeat.<br />
  • 68. tuning case<br />Goal<br />decrease full gc frequency and pause time caused by gc.<br />Method<br />decrease response time or requests,difficult;<br />decrease memory used per request,difficult;<br />decrease memory promoted to old every minor<br />gc,it’s ok;<br />
  • 69. tuning case<br />decrease memory promoted to old every minor gc<br />increase new gen space,now new gen space is big,soit does not work;<br />increase survivor space,it maybe works.<br />
  • 70. tuning case<br />increase Survivor space<br />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;<br />so add -XX:-UseAdaptiveSizePolicy;<br />caculate survivor spaceneed size<br />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;<br />sum many times then average;<br />adjust survivor space to desired size;<br />after the adjustion,minorgcmore,but full gc decrease to every two hours.<br />
  • 71. tuning case<br />the method decrease full gcfrequency,but the pause time caused by gc only decrease 10%;<br />keep Survivorspace,and use cms,the final startup args:<br />-Xms1536m -Xmx1536m -Xmn700m -XX:SurvivorRatio=7 -XX:+UseConcMarkSweepGC-XX:CMSMaxAbortablePrecleanTime=1000 -XX:+CMSClassUnloadingEnabled-XX:+DisableExplicitGC<br />
  • 72. GC Tuning<br />
  • 73. GC Tuning—common policy<br />decrease full gc frequency–common problem<br />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!<br />
  • 74. GC Tuning—common policy<br />decrease full gc frequency–common problem <br />many objects promotion to old gen;common reason: survivor spacetoo small; response time is too slow;<br /> many memory used by per request;solution: decrease minor gc frequency;<br /> let object collect when minor gc,such as <br /> increase suvivorspace,new gen;<br /> use cmsgc;tuning application;<br />
  • 75. GC Tuning—common policy<br />decrease full gc time<br />common reason:old gen is too big;solution:<br /> parallel gc?decrease heap or old gen; add cpu or upgrade cpu.<br />
  • 76. GC Tuning—common policy<br />decrease minor gc frequency<br />common reasonminor gc too small;<br /> many memory used by per request;solutionincrease heap,new or eden; decrease memory used by per request.<br />
  • 77. GC Tuning—common policy<br />decrease minor gc time<br /> common reason response time too slowwsolutionadd cpu or upgrade cpu; if u use cmsgc,then as much as possible avoid object<br /> allocate or promote to old gen,or change to psgc.<br />
  • 78. GC Tuning<br />GC Tuning is art!<br />The final solution is decrease <br />response time or memory used per <br />request;<br />or upgrade to 64 bit,then use large<br />heap,of course it needs more cpu.<br />
  • 79. We’ll learn<br />gc can be used in jdk 1.6<br />how to use these gc<br />what difference between these gc<br />when gc will be executed<br />how to solve oom<br />how to monitor gc<br />how to tuning gc<br />how to realiazegc<br />√<br />√<br />√<br />√<br />√<br />√<br />√<br />
  • 80. Garbage Collection<br />1. usually use below two methods:1.1 Reference counter<br />not suitable for complex object reference situations;<br />count will decrease performance;<br />good point is when counters decrease to zero,then collect.<br /> 1.2 Tracing<br />hotspot use;<br />need pause the application.<br /> common algithorm: Copying/Mark-Sweep/Mark-Compact<br />
  • 81. Garbage Collection<br />Hotspot scan objects from root set;<br />root set<br />1. current running thread;<br />2. public or static variables;<br />3. JVM Handles;<br />4. JNIHandles;<br />
  • 82. Garbage Collection<br />how to pause thread?safepointfirst: test a memory page readable;<br />keypoint:only the code will cause the reference change need<br /> pause;<br />so when gcoccurs,it’ll submit a request to core,then core <br /> set the memory page cannot read,when code execute reach<br />safepoint,then pause.<br />
  • 83. Minor GC<br />Serial<br />Parallel Scavenge<br />ParNew<br />
  • 84. Minor GC<br />allocate on a continous space and use bump the pointer.<br />A<br />B<br />C<br />D<br />
  • 85. Minor GC<br />the policy:<br />scan new gen objects,<br /> find alive objects;<br />use copying algorithm<br /> to finish collection.<br />
  • 86. Minor GC<br />because minor gc only scan new gen,if old gen references new <br />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 <br /> remember set;when minor gc,the objects in remember set also put into<br /> root set.<br />
  • 87. Major GC<br />Serial MSC<br />Parallel MSC<br />CMS<br />Parallel Compacting<br />
  • 88. Major GC—Serial<br />Memory allocation policy<br />1. not support TLAB;<br />2. bump pointer.<br />
  • 89. Major GC—Serial<br />Garbage Collection Policy<br />based on Mark Sweep Compact algorithm.<br />split four phases:<br />1. mark which object alive;<br />2. caculate new address;<br />3. update pointer to new address;<br />4. move object to new address.<br />
  • 90. Major GC—Parallel MSC<br />Memory allocation policy<br />bump pointer.<br />
  • 91. Major GC—Parallel MSC<br />The only difference with serial is multi threads.<br />
  • 92. Major GC—Parallel Compacting<br />Memory allocation is same as parallel MSC.<br />
  • 93. Major GC—Parallel Compacting<br />Garbage Collection policy:<br />
  • 94. Major GC—CMS<br />Memory Allocation Policy<br />1. first get freelist lock;<br />2. find which chunk can put the object;<br />3. if now gc in marking phase,the mark the object alive.<br />
  • 95. Major GC—CMS<br />start a cms thread first;<br />based on Mark-Sweep,split into five phase;<br /> Initial Marking(Stop-the-world)Concurrent MarkingPreClean(Sun jdk 1.5 added)Final Marking(Stop-the-world)<br /> Concurrent Sweeping<br />
  • 96. Major GC—CMS<br />Initial Marking (Stop-the-world)markroot setdirect ref objects;<br />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.<br />
  • 97. Major GC—CMS<br />Preclean <br />scan again phase 2 marked objects and dirty objects;-XX: CMSScheduleRemarkEdenSizeThreshold、-XX: CMSScheduleRemarkEdenPenetration<br />this step maybe causes bug,when object need allocate old,<br /> but remark phase not execute,then old space not enough,<br /> so current step have a timeout arg,default value is 5000ms,<br /> or use -XX: CMSMaxAbortablePrecleanTime to control. <br />
  • 98. Major GC—CMS<br />Final Marking (Stop-the-world)scan Mod Union Table/Card Table,then remark.<br />Concurrent Sweepingcollect the memory used by “dead” objects.<br />
  • 99. Major GC—CMS<br />good point and weak point;<br />mostly concurrent,so it’ll only pause application little time;<br />float garbage,so need more memory size;<br />memory fragment;<br />fight for cpu,so it not suit for cpu intensive app;<br />multi mark,sogc time is longer then parallel gc;<br />memory allocation need use free list,soit’s slower then bumppointer allocate;<br />concurrent with application,so free list will compete.<br />
  • 100. Major GC—CMS<br />float garbage<br />
  • 101. Study SUN JDK V1.6 GC sources<br />download JDK sources;<br />use Source Insightetc. tools to see<br />SerialGC、ParNewGC<br />GenCollectedHeap::mem_allocate<br />PS GC<br />ParallelScavengeHeap::mem_allocate<br />CMS GC<br />ConcurrentMarkSweepThread::run<br />CMSCollector::collect<br />
  • 102. We’ll learn<br />gc can be used in jdk 1.6<br />how to use these gc<br />what difference between these gc<br />when gc will be executed<br />how to solve oom<br />how to monitor gc<br />how to tuning gc<br />how to realiazegc<br />√<br />√<br />√<br />√<br />√<br />√<br />√<br />√<br />
  • 103. References<br />1. GC Tuning in the Hotspot<br />2. Our Collectors<br />3. CMS GC<br />4. why now<br />5. hotspot 6.0 gc tuning<br />6. memory management in hotspot whitepaper<br />

×