Sun jdk 1.6 gc english version
-
Upload
bluedavy-lin -
Category
Documents
-
view
8.541 -
download
4
description
Transcript of Sun jdk 1.6 gc english version
Sun JDK 1.6 GC( Garbage Collector )
bluedavy http://blog.bluedavy.com2010-05-13 V0.2
2010-05-19 V0.52010-06-21 V0.82010-08-03 V0.9
Java : Auto Memory Management why need learn GC ?
OOM? An unexpected long pause ?GC occur frequently?
We’ll learn
gc can be used in jdk 1.6how to use these gcwhat difference between these gcwhen gc will be executedhow to solve oomhow to monitor gchow to tuning gchow to realiaze gc
GC : Garbage Collector
not only for gc collection
also for memory allocate
PC register
local var
oper stack
stack frame
JVM method stack heap
local method stack
method area
-Xss
-XX:PermSize –XX:MaxPermSize
-Xms -Xmx
Basic Concept—runtime data area
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
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
Basic concept—garbage collection
A research report provided by IBM shows about 80% -- 95%objects are temp objects,so Hotspot split the heap to two generations.
Basic concept—generations
Eden S0 S1 Old Generation
New Generation
-XX:SurvivorRatio
-Xmn
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).
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.
Basic concept—two generations
some tips:1 、 when system running,we can use jstat –gcutil to 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.
how many minor gc type ?
& full gc type ?
Serial Parallel ScavengeParNew
Minor GC
Serial MSC Parallel MSC
Major GC
CMSParallel Compacting
We’ll learn
gc can be used in jdk 1.6how to use these gcwhat difference between these gcwhen gc will be executedhow to solve oomhow to monitor gchow to tuning gchow to realiaze gc
√
which one should I choose?
Serial Parallel ScavengeParNew
Minor GC
Minor GC—Serial
1. client class machine default,also can use -XX:+UseSerialGC;
2. eden/s0/s1 capacity use -XX:SurvivorRatio to control, default value is 8,meaning is eden:s0.
mostly allocate on TLAB or 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
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
Minor GC—Serial
The example executes cause one minor gc and one full gc,becausethis rule:
before gc,serial gc will test if average space when minor gc from eden promotion to old > old gen free space, if true,then full,if false,then base on HandleProtomotionFailure value.
Minor GC—Serial
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"); bytes=null; 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]; bytes4=null; bytes5=null; bytes6=null; System.out.println("step 3"); byte[] bytes7=new byte[1024*1024*2]; Thread.sleep(3000); }}
-Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC
-Xms20M –Xmx20M –Xmn10M -XX:-HandlePromotionFailure –XX:+UseSerialGC
Minor GC—Serial
The example execute different when use two type args,becausethis rule:
when minor gc occurs:average space when minor gc from eden promotion to old < old gen free space < eden+from capacityif HandlePromotionFailure=true,then minor gc occurs,if false,then full gc occurs.
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.
Minor GC—Serial
public class SerialGCThreshold{ public static void main(String[] args) throws Exception{ SerialGCMemoryObject object1=new SerialGCMemoryObject(1); SerialGCMemoryObject object2=new SerialGCMemoryObject(8); SerialGCMemoryObject object3=new SerialGCMemoryObject(8); SerialGCMemoryObject object4=new SerialGCMemoryObject(8); object2=null; object3=null; SerialGCMemoryObject object5=new SerialGCMemoryObject(8); Thread.sleep(4000); object2=new SerialGCMemoryObject(8); object3=new SerialGCMemoryObject(8); object2=null; object3=null; object5=null; SerialGCMemoryObject object6=new SerialGCMemoryObject(8); Thread.sleep(5000); }}class SerialGCMemoryObject{ private byte[] bytes=null; public SerialGCMemoryObject(int multi){ bytes=new byte[1024*256*multi]; }}
-Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC
-Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC
-XX:MaxTenuringThres
hold=1
Minor GC—Serial
change the object1 code: SerialGCMemoryObject object1=new SerialGCMemoryObject(2);
-Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC
Minor GC—Serial
The example shows object1 direct to old when the second minor gcoccurs, 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.
Minor GC—Serial
[GC [DefNew: 11509K->1138K(14336K), 0.0110060 secs] 11509K->1138K(38912K),
0.0112610 secs] [Times: user=0.00 sys=0.01, real=0.01 secs]
Minor GC—Serial
Minor GC—ParNew1. when use cms gc,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 differenceis ParNew use multi threads to gc,but if add -XX:+UseAdaptiveSizePolicy,then many differences.
Minor GC—ParNew
[GC [ParNew: 11509K->1152K(14336K), 0.0129150 secs] 11509K->1152K(38912K),
0.0131890 secs] [Times: user=0.05 sys=0.02, real=0.02 secs]
if add -XX:+UseAdaptiveSizePolicy to startup args,then output: [GC [ASParNew: 7495K->120K(9216K), 0.0403410 secs] 7495K-
>7294K(19456K), 0.0406480 secs] [Times: user=0.06 sys=0.15, real=0.04 secs]
Minor GC—PS1. server class machine default,also can add
-XX:+UseParallelGC to use;
2. 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.
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
Minor GC—PS
The example shows bytes4 object allocate on old gen, becausethis rule:
when allocate fail on TLAB 、 eden,test if allocate space >= half of edenspace,if true,then allocate on old gen.
Minor GC—PS
when eden space is not enough,and not allocate on old,then gc 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
Minor GC—PS
the example shows one minor gc and two full gc,because this rule:
1. before gc,serial gc will test if average space when minor gc from eden promotion to old > old gen free space, if true,then full;
2. after gc,also do above test.
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 is false,meaning the alive objects
will 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 on InitialTenuringThreshold,but will be adjusted after every minor gc,if not UseAdaptiveSizePolicy,then based on
MaxTenuringThreshold.
2. to space is not enough,then direct to old gen.
Minor GC—PS
After minor gc,if UseAdaptiveSizePolicy,it’ll adjust eden space and to space based on runtime data,if don’t want to be adjusted,then add-XX:-UseAdaptiveSizePolicy to startup args.
Minor GC—PS
[GC [PSYoungGen: 11509K->1184K(14336K)] 11509K->1184K(38912K), 0.0113360 secs]
[Times: user=0.03 sys=0.01, real=0.01 secs]
Minor GC—PS
which one should I choose?
Serial MSC Parallel MSC
Major GC
CMSParallel Compacting
Major GC—Serialclient class machine default,also can add -XX:+UseSerialGCto startup args to use.
when occurs1. old gen is 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.
Major GC—Serial
[Full GC [Tenured: 9216K->4210K(10240K), 0.0066570 secs] 16584K->4210K(19456K), [Perm : 1692K->1692K(16384K)], 0.0067070 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Major GC—Serial
Major GC—Parallel MSC1. 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.
occur rule is same as Serial.
note: if ScavengeBeforeFullGC is true(the default value), then execute minor GC first.
Major GC—Parallel MSC
[Full GC [PSYoungGen: 1208K->0K(8960K)] [PSOldGen: 6144K->7282K(10240K)] 7352K->7282K(19200K) [PSPermGen: 1686K->1686K(16384K)], 0.0165880 secs] [Times: user=0.01 sys=0.01, real=0.02 secs]
Major GC—Parallel MSC
Major GC—Parallel Compacting
1. can add -XX:+UseParallelOldGC to use;
2. parallel threads: cpu core<=8 ? cpu core : 3+(cpu core*5)/8 or use -XX:ParallelGCThreads=x to set.
occur rule is same as Parallel MSC.
Major GC—Parallel Compacting
[Full GC [PSYoungGen: 1224K->0K(8960K)] [ParOldGen: 6144K->7282K(10240K)] 7368K->7282K(19200K) [PSPermGen: 1686K->1685K(16384K)], 0.0223510 secs] [Times: user=0.02 sys=0.06, real=0.03 secs]
Major GC—Parallel Compacting
Major GC—CMS1. can add -XX:+UseConcMarkSweepGC to use;2. mostly concurrent;3. concurrent threads: (parallel gc threads +3)/4 or use –XX:ParallelCMSThreads=X to set.
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;
MinHeapFreeRatio default value: 40 CMSTriggerRatio default 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; MinHeapFreeRatio default value: 40 CMSTriggerPermRatio default value: 80
or use CMSInitiatingPermOccupancyFraction to set;
Major GC—CMS
occur rule
3. Hotspot decide when to execute CMS GC based on runtime data; or to use -XX:+UseCMSInitiatingOccupancyOnly to disable this rule;
4. the code call System.gc when set ExplicitGCInvokesConcurrent to true; note: when use this and NIO,maybe occur the bug.
Major GC—CMS
public class CMSGCOccur{
public static void main(String[] args) throws Exception{ byte[] bytes=new byte[1024*1024*2]; byte[] bytes1=new byte[1024*1024*2]; byte[] bytes2=new byte[1024*1024*2]; byte[] bytes3=new byte[1024*1024*1]; byte[] bytes4=new byte[1024*1024*2]; Thread.sleep(5000); }
}
-Xms20M –Xmx20M –Xmn10M -XX:+UseConcMarkSweepGC -XX:+UseCMSInitiatingOccupancyOnly -XX:+PrintGCDetails
-Xms20M –Xmx20M –Xmn10M -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails
Major GC—CMS
1. Promotion Failed when minor gc,to space is not enough,then promotion to
old,but old is not enough too,then promotion failed; solution: increase to space,increase old space, or decrease cms gc occur percentage.
2. Concurrent mode failure when need allocate on old,but cms gc is running,then concurrent mode failure,and to keep safe,hotspot will execute serial full gc;
solution:increase old space, or decrease cms gc occur percentage.
Major GC—CMS
[GC [1 CMS-initial-mark: 13433K(20480K)] 14465K(29696K), 0.0001830 secs] [Times: user=0.00 sys=0.00, real=0.00 secs][CMS-concurrent-mark: 0.004/0.004 secs] [Times: user=0.01 sys=0.00, real=0.01 secs] [CMS-concurrent-preclean: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] CMS: abort preclean due to time [CMS-concurrent-abortable-preclean: 0.007/5.042 secs] [Times: user=0.00 sys=0.00, real=5.04 secs] [GC[YG occupancy: 3300 K (9216 K)][Rescan (parallel) , 0.0002740 secs][weak refs processing, 0.0000090 secs] [1 CMS-remark: 13433K(20480K)] 16734K(29696K), 0.0003710 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [CMS-concurrent-sweep: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [CMS-concurrent-reset: 0.000/0.000 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
when add -XX:+UseAdaptiveSizePolicy,then output CMS will change to ASCMS
CMS GC Log
Major GC—CMS
GC—Default
minor gc major gc
Client serial serial
Server ps parallel MSC( after JDK 5.0 Update 6)
GC—Collocation
minor gc major gc
-XX:+UseSerialGC serial serial
-XX:+UseParallelGC ps parallel MSC
-XX:+UseConcMarkSweepGC ParNew CMS
-XX:+UseParNewGC ParNew Serial
-XX:+UseParallelOldGC PS parallel Compacting
-XX:+UseConcMarkSweepGC
-XX:-UseParNewGC
serial CMS
not support 1. -XX:+UseParNewGC –XX:+UseParallelOldGC
2. -XX:+UseParNewGC –XX:+UseSerialGC
GC common args (-Xms –Xmx –Xmn –XX:PermSize –XX:MaxPermSize)
minor GC serial-XX:SurvivorRatio ;
-XX:MaxTenuringThreshold ;PS
-XX:InitialSurvivorRatio ;
-XX:SurvivorRatio ;
-XX:-UseAdaptiveSizePolicy ;
-XX:ParallelGCThreads 。ParNew same as serial;
major GC serial -
parallel GC(including
MSC 、Compacting)
-XX:ParallelGCThreads 。-XX:+ScavengeBeforeFullGC;
CMS-XX:ParallelCMSThreads ;
-XX:CMSInitiatingOccupancyFraction ;
-XX:+UseCMSInitiatingOccupancyOnly ;
-XX:+UseCMSCompactAtFullCollection ;
-XX:CMSMaxAbortablePrecleanTime=X ;-XX:+CMSClassUnloadingEnabled;
We’ll learn
gc can be used in jdk 1.6how to use these gcwhat difference between these gcwhen gc will be executedhow to solve oomhow to monitor gchow to tuning gchow to realiaze gc
√√√√
question: how to let minor gc use ps gc,and major gc use parallel compacting gc?
question:what is difference between ps gc and parnew gc?
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.
Let’s solve some OOM Case
OOM Cases1 、 java -Xms20m -Xmx20m -Xmn10m -XX:+UseParallelGC
com. bluedavy.oom.JavaHeapSpaceCase1
2 、 java -Xms20m -Xmx20m -Xmn10m -XX:+HeapDumpOnOutOfMemoryError com.bluedavy.oom.JavaHeapSpaceCase2
3 、 execute com.bluedavy.oom.JavaHeapSpaceCase3
4 、 execute com.bluedavy.oom.JavaHeapSpaceCase4
5 、 java -Xms1536m -Xmx1536m -Xss100m com.bluedavy.oom.CannotCreateThreadCase
OOM type
the above examples show :
1 、 java.lang.OutOfMemoryError: GC overhead limit exceeded2 、 java.lang.OutOfMemoryError: Java heap space3 、 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 OOM will cause Java process exit; monitor who use native memory: google-perftools5 、 java.lang.OutOfMemoryError: PermGen space PermGen is not enough;
OOM solutionJava Heap Space OOM: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. PermGen too small?2. too many ClassLoaders?
GC Monitor
1. jstat –gcutil [pid] [intervel] [count]
2. -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -Xloggc:[file]
gc log can be analised by GCLogViewer or gchisto.
3. if support GUI,then can use visualvm.
who consume the memory
1. alive long situation,easy,jmap –dump,then MAT;
2. alive short,if no GUI,then in trouble,if GUI,then jprofiler/visualvm.
We’ll learn
gc can be used in jdk 1.6how to use these gcwhat difference between these gcwhen gc will be executedhow to solve oomhow to monitor gchow to tuning gchow to realiaze gc
√√√√√√
java.lang.OutOfMemoryError: Java Heap Space,what can we do?
pls tell me how many minor gc and full gc the app executes.
tuning case 4 cpu , os: linux 2.6.18 32 bit startup args
◦ -server -Xms1536m -Xmx1536m –Xmn700m gc log:
67919.817: [GC [PSYoungGen: 588706K->70592K(616832K)] 1408209K->906379K(1472896K), 0.0197090 secs] [Times: user=0.06 sys=0.00, real=0.02 secs]
67919.837: [Full GC [PSYoungGen: 70592K->0K(616832K)] [PSOldGen: 835787K->375316K(856064K)] 906379K->375316K(1472896K) [PSPermGen: 64826K->64826K(98304K)], 0.5478600 secs] [Times: user=0.55 sys=0.00, real=0.55 secs]
tuning case
minor gc info
tuning casegc log:
◦ 68132.862: [GC [PSYoungGen: 594736K->63715K(609920K)] 1401225K->891090K(1465984K), 0.0309810 secs] [Times: user=0.06 sys=0.01, real=0.04 secs]
◦ 68132.893: [Full GC [PSYoungGen: 63715K->0K(609920K)] [PSOldGen: 827375K->368026K(856064K)] 891090K->368026K(1465984K) [PSPermGen: 64869K->64690K(98304K)], 0.5341070 secs] [Times: user=0.53 sys=0.00, real=0.53 secs]
then repeat.
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;
tuning case
decrease memory promoted to old every minor gc◦increase new gen space,now new gen space is
big,so it does not work;◦increase survivor space,it maybe works.
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 space need 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,minor gc more,but full gc decrease
to every two hours.
tuning case
the method decrease full gc frequency,but the pause time caused by gc only decrease 10% ;
keep Survivor space,and use cms,the final startup args:◦ -Xms1536m -Xmx1536m -Xmn700m -
XX:SurvivorRatio=7 -XX:+UseConcMarkSweepGC -XX:CMSMaxAbortablePrecleanTime=1000 -XX:+CMSClassUnloadingEnabled -XX:+DisableExplicitGC
GC Tuning
1. current status
2. set goal
3. try tuning4. measure
5. adjust
GC Tuning—common policy
decrease full gc frequency – common problem
old gen used too high; common reason: cache too many objects; for example: preparedstatement cache set too big when use oracle driver;
solution: dump then mat,bingo!
decrease full gc frequency – common problem
many objects promotion to old gen; common reason: survivor space too 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 suvivor space,new gen; use cms gc;
tuning application;
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.
GC Tuning—common policy
decrease minor gc frequency
common reason minor gc too small;
many memory used by per request; solution increase heap,new or eden; decrease memory used by per request.
GC Tuning—common policy
decrease minor gc time
common reason response time too sloww solution add cpu or upgrade cpu; if u use cms gc,then as much as possible avoid object
allocate or promote to old gen,or change to ps gc.
GC Tuning—common policy
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 largeheap,of course it needs more cpu.
We’ll learn
gc can be used in jdk 1.6how to use these gcwhat difference between these gcwhen gc will be executedhow to solve oomhow to monitor gchow to tuning gchow to realiaze gc
√√√√√√√
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
Hotspot scan objects from root set;
root set1. current running thread;2. public or static variables;3. JVM Handles;4. JNI Handles;
Garbage Collection
how to pause thread?
safepoint first: test a memory page readable;
keypoint:only the code will cause the reference change need pause;
so when gc occurs,it’ll submit a request to core,then core set the memory page cannot read,when code execute reach safepoint,then pause.
Garbage Collection
Serial Parallel ScavengeParNew
Minor GC
allocate on a continous space and use bump the pointer.
A B C D
Minor GC
the policy: scan new gen objects, find alive objects; use copying algorithm to finish collection.
Minor GC
Minor GCbecause 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,if true,then record to remember set;
when minor gc,the objects in remember set also put into root set.
Serial MSC Parallel MSC
Major GC
CMSParallel Compacting
Major GC—SerialMemory allocation policy
1. not support TLAB;
2. bump pointer.
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.
Major GC—Serial
Major GC—Parallel MSCMemory allocation policy
bump pointer.
The only difference with serial is multi threads.
Major GC—Parallel MSC
Major GC—Parallel Compacting
Memory allocation is same as parallel MSC.
Garbage Collection policy:
Major GC—Parallel Compacting
Major GC—CMSMemory 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.
1. start a cms thread first;
2. 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
Major GC—CMS
1. Initial Marking (Stop-the-world) mark root set direct ref objects;
2. Concurrent Marking concurrent mark phase 1 marked objects recursively; Mod Union Table
to solve the ref change when minor gc executes; Card Table to solve the ref change in the old gen.
Major GC—CMS
3. 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.
Major GC—CMS
4. Final Marking (Stop-the-world) scan Mod Union Table/Card Table,then remark.
5. Concurrent Sweepingcollect the memory used by “dead” objects.
Major GC—CMS
good point and weak point;
1. mostly concurrent,so it’ll only pause application little time;2. float garbage,so need more memory size;3. memory fragment;4. fight for cpu,so it not suit for cpu intensive app;5. multi mark,so gc time is longer then parallel gc;6. memory allocation need use free list,so it’s slower then bump
pointer allocate;7. concurrent with application,so free list will compete.
Major GC—CMS
float garbage
Major GC—CMS
Study SUN JDK V1.6 GC sources
download JDK sources;use Source Insight etc. tools to see
◦SerialGC 、 ParNew GC GenCollectedHeap::mem_allocate
◦PS GC ParallelScavengeHeap::mem_allocate
◦CMS GC ConcurrentMarkSweepThread::run CMSCollector::collect
We’ll learn
gc can be used in jdk 1.6how to use these gcwhat difference between these gcwhen gc will be executedhow to solve oomhow to monitor gchow to tuning gchow to realiaze gc
√√√√√√√√
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