-
Hotspot JVM and GC basics study note
-
JVM components
-
HotSpot JVM comprises three main components: the class loader, the runtime data areas and the execution engine.
-
Key JVM components
There are three key components related to tune performance: the heap, the JIT compiler and the garbage collector. However, the JIT compiler is usually used to generate dynamically more efficient the native machine code. So it doesn't need to be tuned during running process with the latest versions of the JVM.
JVM tuning performance is focused on setting a proper heap size and choosing the most appropriate garbage collector.
-
Performance targets
Responsiveness and throughput are two basic targets for performance.
-
Responsiveness: how much time a server must response to the requests from the clients, e.g.: a web server returning to the responses in less than three seconds, a database server sending query results in three hundreds milliseconds. Quick response time is the most important consideration for responsiveness.
-
Throughput: Focus on maximizing the amount of job by an application in a specific period of time. I.E. the max number of transactions which an application can deal with in an hour. High pause times can be accepted for the performance target.
-
Automatic garbage collection
Automatic garbage collection is looking at heap memory, finding which objects are used and which ones are not used. Delete the unused objects and deallocate them. The memory used can be reclaimed.
-
Basic process
-
Step1: marking
The process marks which objects are unreferenced and which objects are referenced.
-
Step2: Normal deletion
Remove the unreferenced objects and leave referenced objects and free memory pointers.
Step2a: Deletion with compacting
In addition to remove unreferenced objects, move the referenced objects together. Make new memory reallocation easier and faster.
-
Why generational garbage collection
Y axis: the amount of bytes allocated;
X axis: X access shows the total numbers of bytes over time.(X axis represents time)
Empirical analysis of applications show most of the objects have a short lived.
-
JVM generations
-
The young generation is where all new objects are generated and aged. The young generation's filling up triggers a minor garbage collection which is a kind of 'stop the world ' events.
-
The old generation is used to save the long survival objects, which cause a major garbage collection, another kind of 'stop the world' events.
-
The permanent generation contains metadata required by JVM to describe the classes and methods used by the application. In addition, JDK library classes and methods may be saved here. A full garbage collection includes permanent generation when a classes and methods get collected or unloaded.
-
Generational garbage collection process
-
First, new objects are allocated in Eden space. Both survivals space are empty.
1,3 represents the object's age.
-
A minor garbage collection is triggered when the Eden space fills up.
-
Referenced objects are moved to S0, the first survivor, whereas unreferenced objects are removed. The Eden space is cleared.
-
At the next minor GC, the same process happens in the Eden space, however, there is one difference from the last minor GC, which is that the referenced objects are moved to the S1 space instead of the S0. Furthermore, the referenced objects' ages in S0 are added by 1 then are moved to the S1 also. Finally, both the Eden space and the S0 space are cleared.
-
Next, the same process repeats except that S0 replaces of S1 to save the referenced objects.
-
This slide demonstrates the promotion from the young generation to the old generation. After a minor GC, when the age reaches the threshold(8 in the example), the referenced objects are promoted into the old generation.
-
As minor GC continue to occur, the objects will continue to be promoted from the Eden space to the old space.
-
The above steps cover the whole process with the young generation. Eventually, the major GC will be performed on the old generation which cleans up and compact the space.
-
Java garbage collectors
-
Common Heap Related Switches
-
Switches | Description |
-Xmx | Set the maximum heap size |
-Xms | Set the initial heap size |
-Xmn | Set the size of the young generation |
-XX:PermSize | Set the starting size of the permanent generation |
-XX:MaxPermSize | Set the maximum of the permanent generation |
-
The Serial GC
The default GC collector is used by client style machine in JavaSE5 and 6. Both minor and major GC are done serially by a single visual CPU. It uses mark and compact collection method.
-
Use cases
Applications don't have low pause requirement. Client-style machines. One machine runs many JVMs.
-
Command line switches
To enable the serial GC: -XX:+UseSerialGC
E.g. java –Xmx –Xms –Xmn –XX:PermSize=20m –XX:MaxPermSize=20m –XX:UseSerialGC –jar c:\javademos\demo\jfc\Java2D\Java2demo.jar
-
The Parallel GC
Uses the multiple thread to do the young generation garbage collection. By default, on the N CPUs machine it uses N parallel garbage collector threads to do the collection. The amount of the parallel threads can be controlled by the command-line options:
-XX:ParallelGCThreads=<desired number>
-
Use cases
This collector is called a throughput collector, which is used in a high throughput requirement environment with long pauses accepted.
-
Command line swithes
-XX:UseParallelGC
Use multi-thread young generation collector, single-thread old generation collector with a compaction of old generation.
E.g. java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseParallelGC -jar c:\javademos\demo\jfc\Java2D\Java2demo.jar
-XX:UseParallelOldGC
Both young generation and old use multi-thread collectors. Only old generation GC uses compaction method.
E.g. java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseParallelOldGC -jar c:\javademos\demo\jfc\Java2D\Java2demo.jar
-
The Concurrent Mark Sweep(CMS) Collector
The concurrent mark sweep collector also refers to as the concurrent low pause collector, which is used to collector the tenured generation, running at the same time with applications. It uses the same algorithm as the parallel collector on the young generation.
-
Use cases
Require low pauses and share resources with applications.
-
Command line switches
To enable the CMS collector: -XX:+UseConcMarkSweepGC
Set the number of threads: -XX:ParrallelCMSThread=<desired number>
E.g. java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseConcMarkSweepGC -XX:ParallelCMSThreads=2 -jar c:\javademos\demo\jfc\Java2D\Java2demo.jar
-
The G1 Garbage Collector
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/G1GettingStarted/index.html
The G1 is available in Java 7 and is purposed to instead of the CMS collector, which is concurrent, parallel, low-pause collector. I'll make note for the details about G1 in the following paragraph.
-
Command line switches
Enable G1: -XX:+UseG1GC
E.g. java -Xmx12m -Xms3m -XX:+UseG1GC -jar c:\javademos\demo\jfc\Java2D\Java2demo.jar
In conclude, the OBE(Oracle by Example) includes:
-
JVM components: key components for tuning JVM.
-
Performance target: responsiveness and throughput.
-
Automatic garbage collection process.
-
Java garbage collectors: the serial GC, the parallel GC, the Concurrent Mark Sweep GC and G1.
-
Collectors Comparision
The serial collector
The parallel collector
The concurrent mark sweep
The G1
Stop the World
Yes
Support Minor garbage collection
Support Major garbage collection
Support Full garbage collection
Compact
Command line
Use case
-
Comparisons of Three types of GC
Minor GC
Major GC
Full GC
Stop the World
Y
The trigger point
When free spaces can't be allocated for new objects
Minor GC often triggers major GC
Process
1,Copy Objects referenced From eden, survivor to another survivor.
2,Clean up eden, survivor.
Latency issues
1, If there are lots of live objects in young generation heap, minor gc will cost much more time
-