jvm基本概要及调优

代码精灵
• 阅读 1108

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收集器

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

G1收集器

jvm基本概要及调优
G1收集器是基于“标记-整理”算法实现的收集器,也就是说它不会产生空间碎片,这对于长时间运行的应用系统来说非常重要。
二是它可以非常精确地控制停顿,既能让使用者明确指定在一个长度为M毫秒的时间片段内,消耗在垃圾收集上的时间不得超过N毫秒
为了达到低 pause time ,G1对堆内存的分配做了如下变动
jvm基本概要及调优
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.

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
3年前
JAVA工程师成神道路
一、基础篇1.1JVM1.1.1.Java内存模型,Java内存管理,Java堆和栈,垃圾回收http://www.jcp.org/en/jsr/detail?id133http://ifeve.com/jmmfaq/1.1.2.了解JVM各种参数及调优1.1.3.
Wesley13 Wesley13
3年前
Java工程师成神之路~
一、基础篇1.1JVM1.1.1.Java内存模型,Java内存管理,Java堆和栈,垃圾回收http://www.jcp.org/en/jsr/detail?id133http://ifeve.com/jmmfaq/1.1.2.了解JVM各种参数及调优1.1.3.
Wesley13 Wesley13
3年前
Java工程师成神之路
一、基础篇1.1JVM1.1.1.Java内存模型,Java内存管理,Java堆和栈,垃圾回收http://www.jcp.org/en/jsr/detail?id133http://ifeve.com/jmmfaq/1.1.2.了解JVM各种参数及调优
Stella981 Stella981
3年前
JVM系列【6】GC与调优2
JVM系列笔记目录虚拟机的基础概念class文件结构class文件加载过程jvm内存模型JVM常用指令GC与调优了解HotSpot常用命令行参数JVM的命令行参数参考:https://docs.oracle.com/javase/8/docs/
Stella981 Stella981
3年前
JVM参数表
JavaHotSpotVM中\XX:的可配置参数列表进行描述;这些参数可以被松散的聚合成三类:行为参数(BehavioralOptions):用于改变jvm的一些基础行为;性能调优(PerformanceTuning):用于jvm的性能调优;调试参数(DebuggingOptions):一般用
Stella981 Stella981
3年前
JVM 参数及各部分含义(转)
转自:https://www.jianshu.com/p/1c6b5c2e95f9JVM参数分类JVM参数分为标准参数和非标准参数:标准参数:""开头的参数,如client,server等非标准参数:"
Stella981 Stella981
3年前
SpringBoot 深度调优,JVM 调优教程
编辑:业余草来源:https://www.xttblog.com/?p4937项目调优作为一名工程师,项目调优这事,是必须得熟练掌握的事情。在SpringBoot项目中,调优主要通过配置文件和配置JVM的参数的方式进
Stella981 Stella981
3年前
JVM 调优参数解释
典型配置:javaXmx3800mXms3800mXmn2gXss128kXX:UseParallelGCXX:ParallelGCThreads20XX:UseParallelGC:选择垃圾收集器为并行收集器。此配置仅对年轻代有效。
Stella981 Stella981
3年前
JVM系列【6】GC与调优6
JVM系列笔记目录虚拟机的基础概念class文件结构class文件加载过程jvm内存模型JVM常用指令GC与调优GC常用参数\XmnXmsXmxXss年轻代最小堆最大堆栈空间\XX:UseTLAB使用
Stella981 Stella981
3年前
JVM调优之经验
在生产系统中,高吞吐和低延迟一直都是JVM调优的最终目标,但这两者恰恰又是相悖的,鱼和熊掌不可兼得,所以在调优之前要清楚舍谁而取谁。一般计算任务和组件服务会偏向高吞吐,而web展示则偏向低延迟才会带来更好的用户体验。本文从性能和经验上来分享一下JVM参数的设置。调优之前可以先用XX:PrintFlagsFinal来查看虚拟机是否默认开启某
Stella981 Stella981
3年前
JVM参数及调优
调优基本概念在调整JVM性能时,通常有三个组件需要考虑:1.堆大小调整2.垃圾收集器调整3.JIT编译器大多数调优选项都与调整堆大小和选择合适的垃圾收集器有关,JIT编译器对性能也有很大影响,但很少需要对其进行调优,尤其是针对较新版本的JVM。通常,在进行Java程序调优的时候,会重点关注两个主要指标:响应
代码精灵
代码精灵
Lv1
愿山野浓雾都有路灯,风雨漂泊都有归舟。
文章
8
粉丝
0
获赞
0