Hotspot JVM and GC basics studynote(1)

1      Hotspot JVM and GC basics studynote

1.1  JVM components

Hotspot JVM and GC basics studynote(1)_第1张图片

HotSpot JVM comprises three maincomponents: the class loader, the runtime data areas and the execution engine.

1.1.1    Key JVM components

There are three key components related totune performance: the heap, the JIT compiler and the garbage collector.However, the JIT compiler is usually used to generate dynamically moreefficient the native machine code. So it doesn’t need to be tuned duringrunning process with the latest versions of the JVM.

JVM tuning performance is focused on setting a proper heapsize and choosing the most appropriate garbage collector.

1.2  Performance targets

Responsiveness and throughput are two basictargets for performance.

ü  Responsiveness: how much time a server must response to the requestsfrom the clients, e.g.: a web server returning to the responses in less thanthree seconds, a database server sending query results in three hundredsmilliseconds. Quick response time is the most important consideration forresponsiveness.

ü  Throughput: Focus on maximizing the amount of job by an applicationin a specific period of time. I.E. the max number of transactions which anapplication can deal with in an hour. High pause times can be accepted for theperformance target.

1.3  Automatic garbage collection

Automatic garbage collection is looking atheap memory, finding which objects are used and which ones are not used. Deletethe unused objects and deallocate them. The memory used can be reclaimed.

1.3.1    Basic process

1)        Step1: marking

The process marks which objects are unreferencedand which objects are referenced.

2)        Step2: Normal deletion

Remove the unreferenced objects and leave referencedobjects and free memory pointers.

Step2a: Deletion with compacting

In addition to remove unreferenced objects,move the referenced objects together. Make new memory reallocation easier andfaster.

1.3.2    Why generational garbagecollection

Y axis: the amount of bytes allocated;

X axis: X access shows the total numbers ofbytes over time.(X axis represents time)

Empirical analysis of applications showmost of the objects have a short lived.

1.3.3    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, whichcause a major garbage collection, another kind of ‘stop the world’ events.

ü  The permanent generation contains metadata required by JVM todescribe the classes and methods used by the application. In addition, JDK library classesand methods may be saved here. A full garbage collection includespermanent generation when a classes and methods get collected or unloaded.

1.3.4    Generational garbage collectionprocess

1)        First, new objects areallocated in Eden space. Both survivals space are empty.

1,3 represents the object’s age.

2)        A minor garbage collection istriggered when the Eden space fills up.

3)        Referenced objects are moved toS0, the first survivor, whereas unreferenced objects are removed. The Edenspace is cleared.

4)        At the next minor GC, the sameprocess happens in the Eden space, however, there is one difference from thelast minor GC, which is that the referenced objects are moved to the S1 spaceinstead of the S0. Furthermore, the referenced objects’ ages in S0 are added by1 then are moved to the S1 also. Finally, both the Eden space and the S0 spaceare cleared.

5)        Next, the same process repeatsexcept that S0 replaces of S1 to save the referenced objects.

6)        This slide demonstrates thepromotion from the young generation to the old generation. After a minor GC,when the age reaches the threshold(8 in the example), the referenced objectsare promoted into the old generation.

7)        As minor GC continue to occur,the objects will continue to be promoted from the Eden space to the old space.

8)        The above steps cover the wholeprocess with the young generation. Eventually, the major GC will be performedon the old generation which cleans up and compact the space.

1.4  Java garbage collectors

1.4.1    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

1.4.2    The Serial GC

The default GC collector is used by client stylemachine in JavaSE5 and 6. Both minor and major GC are done seriallyby a single visual CPU. It uses mark and compact collection method.

ü  Use cases

Applications don’t have low pauserequirement. 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

1.4.3    The Parallel GC

Uses the multiple thread to do the younggeneration garbage collection. By default, on the N CPUs machine it uses Nparallel garbage collector threads to do the collection. The amount of theparallel threads can be controlled by the command-line options:

-XX:ParallelGCThreads=

ü  Use cases

This collector is called a throughputcollector, which is used in a high throughput requirement environment with longpauses accepted.

ü  Command line swithes

-XX:UseParallelGC

Use multi-thread young generationcollector, single-thread old generation collector with a compaction of oldgeneration.

E.g. java -Xmx12m -Xms3m -Xmn1m-XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseParallelGC -jarc:\javademos\demo\jfc\Java2D\Java2demo.jar

-XX:UseParallelOldGC

Both young generation and old usemulti-thread collectors. Only old generation GC uses compaction method.

E.g. java -Xmx12m -Xms3m -Xmn1m-XX:PermSize=20m -XX:MaxPermSize=20m -XX:+UseParallelOldGC -jarc:\javademos\demo\jfc\Java2D\Java2demo.jar

1.4.4    The Concurrent Mark Sweep(CMS)Collector

The concurrent mark sweep collector also refersto as the concurrent low pause collector, which is used to collector thetenured generation, running at the same time with applications. It uses thesame algorithm as the parallel collector on the young generation.

ü  Use cases

Require low pauses and share resources withapplications.

ü  Command line switches

To enable the CMS collector:-XX:+UseConcMarkSweepGC

Set the number of threads: -XX:ParrallelCMSThread=

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

1.4.5    The G1 Garbage Collector

The G1 is available in Java 7 and ispurposed to instead of the CMS collector, which is concurrent, parallel,low-pause collector. I’ll make note for the details about G1 in the followingparagraph.

ü  Command line switches

Enable G1: -XX:+UseG1GC

E.g. java -Xmx12m -Xms3m -XX:+UseG1GC -jarc:\javademos\demo\jfc\Java2D\Java2demo.jar

In conclude, the OBE(Oracle by Example)includes:

1)        JVM components: key componentsfor tuning JVM.

2)        Performance target:responsiveness and throughput.

3)        Automatic garbage collectionprocess.

4)        Java garbage collectors: theserial GC, the parallel GC, the Concurrent Mark Sweep GC and G1.

 

 

你可能感兴趣的:(Java技术)