for each objectin allocatedObjectList:
clearing the mark bit
// 所以对象是8字节的倍数,遍历还可以跳着来
DFS starting from GC-Roots:
set the reached object mark bit
for each objectin allocatedObjectList:
if mark bit hasn't setted:
remove it from allocatedObjectList
Prints the wallclock time that GC events occured at.
-XX:+PrintGCDateStamps
Prints the time (in secs since VM start) that GC events occured at.
-XX:+PrintGCTimeStamps
Adds extra GC event detail that is vital for tooling
-XX:+PrintTenuringDistribution
Switches on log file rotation
-XX:+UseGCLogFileRotation
Set the maximum number of log files to keep
-XX:+NumberOfGCLogFiles=< n>
Set the maximum size of each file before rotation
-XX:+GCLogFileSize=< size>
Log分析工具
Censum
GCViewer
基本调优
Table 8-3. GC heap sizing flags
Effect
Flag
Set the minimum size reserved for the heap
-Xms< size>
Set the maximum size reserved for the heap
-Xmx< size>
Set the maximum size permitted for PermGen (Java 7)
-XX:MaxPermSize=< size>
Set the maximum size permitted for Metaspace (Java 8)
-XX:MaxMetaspaceSize=< size>
临界对象大小
-XX:PretenureSizeThreshold=N>
最小TLAB大小
-XX:MinTLABSize=N
GC测试代码
@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@OutputTimeUnit(TimeUnit.SECONDS)
@Fork(1)
publicclassSimulateCardTable {// OldGen is 3/4 of heap, 2M of card table is required for 1G of old genprivatestaticfinalint SIZE_FOR_20_GIG_HEAP = 15 * 2 * 1024 * 1024;
privatestaticfinalbyte[] cards = newbyte[SIZE_FOR_20_GIG_HEAP];
@Setuppublicstaticfinalvoidsetup() {
final Random r = new Random(System.nanoTime());
for (int i=0; i<100_000; i++) {
cards[r.nextInt(SIZE_FOR_20_GIG_HEAP)] = 1;
}
}
@BenchmarkpublicintscanCardTable() {
int found = 0;
for (int i=0; i<SIZE_FOR_20_GIG_HEAP; i++) {
if (cards[i] > 0)
found++;
}
return found;
}
}
/*
Result "scanCardTable":
108.904 ±(99.9%) 16.147 ops/s [Average]
(min, avg, max) = (102.915, 108.904, 114.266), stdev = 4.193
CI (99.9%): [92.757, 125.051] (assumes normal distribution)
# Run complete. Total time: 00:01:46
Benchmark Mode Cnt Score Error Units
SimulateCardTable.scanCardTable thrpt 5 108.904 ± 16.147 ops/s
*/
并发调优
Effect
Flag
(Old flag) Set ratio of YoungGen to Heap
-XX:NewRatio=N
(Old flag) Set ratio of Survivor spaces to YoungGen
-XX:SurvivorRatio=N
(Old flag) Set min size of YoungGen
-XX:NewSize=N
(Old flag) Set max size of YoungGen
-XX:MaxNewSize=N
(Old flag) Set min % of heap free after GC to avoid expanding
-XX:MinHeapFreeRatio
(Old flag) Set max % of heap free after GC to avoid shrinking
-XX:MaxHeapFreeRatio
Flags set:
-XX:NewRatio=N
-XX:SurvivorRatio=K
YoungGen = 1 / (N+1) of heap
OldGen = N / (N+1) of heap
Eden = (K – 2) / K of YoungGen
Survivor1 = 1 / K of YoungGen
Survivor2 = 1 / K of YoungGen