JVM Tuning
很久以前的PPT,乘著最近對JVM的又一次折騰,再整理出來,分享一下。
Hotspot JVM Architecture v.s. Memory Layout Of a Process
了解JVM之前,我們首先可以對操作系統(tǒng)的進程的內(nèi)存布局作個了解,JVM作為操作系統(tǒng)的一個進程,大致上還是遵從了
進程的內(nèi)存布局,并且兩著直接有一定的關(guān)系。


每個方法被執(zhí)行的時候都會同時創(chuàng)建一個棧幀(Stack Frame)用于存儲局部變量表、操作棧、動態(tài)鏈接、方法出口
等信息。每一個方法被調(diào)用直至執(zhí)行完成的過程,就對應(yīng)著一個棧幀在虛擬機棧中從入棧到出棧的過程。

VmLck:正在鎖住的物理內(nèi)存
VmHWM:使用的物理內(nèi)存的峰值
VmRSS:當前正在使用的物理內(nèi)存
VmData:

reference: https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/toc.html
Introduction
- JVM (Java HotSpot virtual machine)提供了多個GC (Garbage Collectors)
- Java SE 可以根據(jù)應(yīng)用選擇合適的GC,但是這種選擇不一定對每個應(yīng)用都是可選的???
- GC 是 內(nèi)存管理的工具, 它可以通過下面的操作來達到自動管理內(nèi)存的目的:
- 將objects 分配到新生代,將較老的objects推入老生代
- 通過并行的標識階段(concurrent/parallel marking phase) 找到old generation 里面的存活的objects。 當總的堆內(nèi)存占有率達到默認的閾值事,JVM將會觸發(fā)這個階段。
- 通過并行拷貝parallel copying, 壓縮live objects來提升free memory。
- 什么時候GC的選擇有著重大的影響呢?
- 對某些應(yīng)用而言,也許沒有任何影響。比如:在gc頻率適度,gc pause的時間可以接受的情況下,應(yīng)用程序能夠運行良好。
- 但是對大多數(shù)應(yīng)用,特別是處理大數(shù)據(jù)量(幾G),多線程,高事務(wù)并發(fā)的應(yīng)用而言,gc的選擇非常重要。
JVM tuning parameters
- Maximum Pause Time:This is interpreted as a hint to the garbage collector that pause times of <nnn> milliseconds or less are desired.
- -XX:MaxGCPauseMillis=<nnn>
- Throughput:The throughput goal is measured in terms of the time spent collecting garbage and the time spent outside of garbage collection (referred to as application time).
- XX:GCTimeRatio=<nnn>.
- The ratio of garbage collection time to application time is 1 / (1 + <nnn>). For example, -XX:GCTimeRatio=19 sets a goal of 1/20th or 5% of the total time for garbage collection.
- -verbose:gc, -XX:+PrintGCDetails,-XX:+PrintGCTimeStamps
- the total heap size
- -Xms<min>
- -Xmx<max>
- -XX:MinHeapFreeRatio=<minimum>
- -XX:MaxHeapFreeRatio=<maximum>
- the young generation size
- NewRatio: the ratio between the young and tenured generation is 1:3 -XX:NewRatio=3
- NewSize:the size from below
- MaxNewSize:the size from above
- Survivor Space Sizing
- SurvivorRatio: -XX:SurvivorRatio=6 sets the ratio between eden and a survivor space to 1:6
- XX:+PrintTenuringDistribution
- Available Collectors
- -XX:+UseSerialGC
- -XX:-UseParallelOldGC
- -XX:+UseConcMarkSweepGC
- -XX:+UseG1GC
- java -XX:+PrintFlagsFinal <GC options> -version | grep MaxHeapSize
JVM調(diào)優(yōu)的目標是什么?
Pauses are the times when an application appears unresponsive because garbage collection is occurring.
Throughput is the percentage of total time not spent in garbage collection considered over long periods of time. Throughput includes time spent in allocation (but tuning for speed of allocation is generally not needed).
If the throughput and maximum pause time goals have been met, then the garbage collector reduces the size of the heap until one of the goals (invariably the throughput goal) cannot be met. The goal that is not being met is then addressed.
-
Maximum Pause Time Goal 設(shè)置最大停頓時間
-XX:MaxGCPauseMillis=<nnn> -
Throughput Goal最大吞吐率目標
- -XX:GCTimeRatio=<nnn>.
- Throughput=1 / (1 + <nnn>)
-XX:GCTimeRatio=19 sets a goal of 1/20th or 5% of the total time for garbage collections
- Footprint (Heap Size)
GC Logs
-XX:PrintGCDetails -Xloggc
eg: -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:/logs/gc.log
https://blog.csdn.net/f59130/article/details/74013460
堆棧大小調(diào)優(yōu)
Xms Xmx NewRatio NewSize MaxNewSize SurvivorRatio
https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/toc.html


如何查看分析JVM問題
gc算法
-
Serial -XX:+UseSerialGC
- Single processor
- Simple and efficient relatively
- Client
- Multiprocessors for applications with small data (<100M)
-
Parallel -XX:+UseParallelGC
- Application with Medium-size/large-size data size
- Throughput goal (Throughput collector)
- Multiprocessors
-
Concurrent-Mark-Sweep -XX:+UseConcMarkSweepGC
- Applications with medium-size/large-size data sets
- Response time is more important than overall throughput
- Only use in old generation
- will be replaced by G1
算法主要步驟:
- Initial mark(STW, single-thread) :Stop all application threads, identify the set of objects reachable from roots, and then resume all application threads.
- Concurrent mark : Concurrently trace the reachable object graph, using one or more processors, while the application threads are executing.
- Concurrent pre-clean[part of remark work] :
- Remark(STW, multi-thread) : Stop all application threads and retrace sections of the roots and object graph that may have been modified since they were last examined, and then resume all application threads.
- Concurrent sweep : Concurrently sweep up the unreachable objects to the free lists used for allocation, using one processor.
- Concurrent reset: Concurrently resize the heap and prepare the support data structures for the next collection cycle, using one processor.
-
G1 -XX:+UseG1GC
g1.png- Garbage-First, partition heap to equally sized regions
- Multiprocessor machines with large memories(heapsize>6g)
- Meets gc pause time goals with high probability while achieving high throughput
- More than 50% of the Java heap is occupied with live data.
- The rate of object allocation rate or promotion varies significantly.
- The application is experiencing undesired long garbage collection or compaction pauses (longer than 0.5 to 1 second).
G1算法主要步驟:
- Initial marking phase (STW)
mark roots, This phase is piggybacked on a normal (STW) young garbage collection. - Root region scanning phase
scans survivor regions marked during the initial marking phase for references to the old generation and marks the referenced objects
concurrently ,must complete before the next STW young garbage collection can start. - Concurrent marking phase
GC finds reachable (live) objects across the entire heap
can be interrupted by STW young garbage collections - Remark phase (STW)
Completes marking (SnapshotAtTheBeginning[SATB] buffer (other GC use dirty card algorithm)) , traces unvisited live objects
Reference processing - Cleanup phase
Liveness accounting and identify completely free regions (STW)
Remembered Set scrubbing (STW)
Reset the empty regions and return them to the free list (Concurrent)
- 如何為選擇GC算法
gc-choices.png

