jvm调优

JVM参数

  • 标准参数:
    不会随JDK版本变化而发生改变 eg:java -version
  • -X参数:
    非标准参数,会随JDK版本变动而变动 eg:-Xint
  • -XX参数:

    1. Boolean类型 eg:-XX[+/-]name 启动/停止
    2. 非Boolean类型 eg: -XX:name=value
    3. -XX:+PrintCommandLineFlags 打印JVM系统启动参数
    4. -XX:+PrintGCDetails 内存信息
    5. -XX:+PrintFlagsFinal 查看系统默认启动参数
  • 其化类型
    系某些参数的简写格式
    -Xms100M===>-XX:initialHeapSize=100M
    -Xmx100M===>-XX:MaxHeapSize=100M

jvm工具

jmap

jmap -heap pid 查看当前JVM使用的GC collector
查看当前JVM默认的配置参数
java -XX:+PrintFlagsFinal -version | grep :
java -XX:+PrintCommandLineFlags -version

jstat

查看GC的一些日志,详情另行查看。

垃圾回收算法

标记 —— 清除算法

  • 效率不高
  • 空间会产生大量碎片

复制算法

把空间分成两块,每次只对其中一块进行 GC。当这块内存使用完时,就将还存活的对象复制到另一块上面

标记-整理算法

不同于针对新生代的复制算法,针对老年代的特点,创建该算法。主要是把存活对象移到内存的一端。

分代回收

根据存活对象划分几块内存区,一般是分为新生代和老年代。然后根据各个年代的特点制定相应的回收算法。

  1. 新生代
    每次垃圾回收都有大量对象死去,只有少量存活,选用复制算法比较合理。
  2. 老年代
    老年代中对象存活率较高、没有额外的空间分配对它进行担保。所以必须使用标记 —— 清除或者标记 —— 整理算法回收。

jvm HotSpot算法实现

Serial收集器

这是一个单线程收集器。意味着它只会使用一个CPU 或一条收集线程去完成收集工作,并且在进行垃圾回收时必须暂停其它所有的工作线程直到收集结束。

Parallel 收集器

实际测试下来 young old eden survior的比例与设置的置并非强一致,差别较大。查看官方文档, 解释如下
The parallel collector is selected by default on server-class machines. In addition, the parallel collector uses a method of automatic tuning that allows you to specify specific behaviors instead of generation sizes and other low-level tuning details. You can specify maximum garbage collection pause time, throughput, and footprint (heap size).

  • Maximum Garbage Collection Pause Time: The maximum pause time goal is specified with the command-line option-XX:MaxGCPauseMillis=`<N>. This is interpreted as a hint that pause times of<N>`milliseconds or less are desired; by default, there is no maximum pause time goal. If a pause time goal is specified, the heap size and other parameters related to garbage collection are adjusted in an attempt to keep garbage collection pauses shorter than the specified value. These adjustments may cause the garbage collector to reduce the overall throughput of the application, and the desired pause time goal cannot always be met.
  • Throughput: The throughput goal is measured in terms of the time spent doing garbage collection versus the time spent outside of garbage collection (referred to as application time). The goal is specified by the command-line option-XX:GCTimeRatio=`<N>, which sets the ratio of garbage collection time to application time to1 / (1 +<N>)`.

    For example,-XX:GCTimeRatio=19sets a goal of 1/20 or 5% of the total time in garbage collection. The default value is 99, resulting in a goal of 1% of the time in garbage collection.

  • Footprint: Maximum heap footprint is specified using the option-Xmx`<N>`. In addition, the collector has an implicit goal of minimizing the size of the heap as long as the other goals are being met.

Priority of Goals

The goals are addressed in the following order:

  1. Maximum pause time goal
  2. Throughput goal
  3. Minimum footprint goal

The maximum pause time goal is met first. Only after it is met is the throughput goal addressed. Similarly, only after the first two goals have been met is the footprint goal considered.

CMS收集器

Screen Shot 2020-03-13 at 01.00.49.png
CMS(Concurrent Mark Sweep)收集器是基于“标记-清除”算法实现的,它使用多线程的算法去扫描堆(标记)并对发现的未使用的对象进行回收(清除)
缺点:CMS是一款基于“标记-清除”算法实现的收集器,这意味着收集结束时会产生大量空间碎片

G1收集器

Screen Shot 2020-03-15 at 00.00.17.png
G1收集器是基于“标记-整理”算法实现的收集器,也就是说它不会产生空间碎片,这对于长时间运行的应用系统来说非常重要。
二是它可以非常精确地控制停顿,既能让使用者明确指定在一个长度为M毫秒的时间片段内,消耗在垃圾收集上的时间不得超过N毫秒
为了达到低 pause time ,G1对堆内存的分配做了如下变动
Screen Shot 2020-03-15 at 00.31.29.png
G1 is generational in a logical sense. A set of empty regions is designated as the logical young generation. In the figure, the young generation is light blue. Allocations are done out of that logical young generation, and when the young generation is full, that set of regions is garbage collected (a young collection). In some cases, regions outside the set of young regions (old regions in dark blue) can be garbage collected at the same time. This is referred to as a_mixed collection_. In the figure, the regions being collected are marked by red boxes. The figure illustrates a mixed collection because both young regions and old regions are being collected. The garbage collection is a compacting collection that copies live objects to selected, initially empty regions. Based on the age of a surviving object, the object can be copied to a survivor region (marked by "S") or to an old region (not specifically shown). The regions marked by "H" contain humongous objects that are larger than half a region and are treated specially; see the sectionHumongous Objects and Humongous AllocationsinGarbage-First Garbage Collector.


xiao_dingo
25 声望2 粉丝

« 上一篇
java内存模型