转载

jvm基本概要及调优

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 to 1 / (1 + <N> )`.

    For example, -XX:GCTimeRatio=19 sets 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收集器

G1收集器

原文  https://segmentfault.com/a/1190000021994363
正文到此结束
Loading...