JVM调优介绍

JVM调优介绍

一、什么是JVM调优

1.根据需求进行JVM规划和预调优

2.优化运行JVM运行环境(慢,卡顿)

3.解决JVM运行过程中出现的各种问题(OOM等)

二、JVM调优是主要调什么

JVM调优主要是减少GC的频率和Full GC次数,STW(stop the world)的停顿时间和次数

三、什么是STW

STW指的是GC事件发生过程中,会产生应用程序的停顿。停顿产生时整个应用程序线程都会被暂停,没有任何响应, 有点像卡死的感觉,这个停顿称为STW。Java中一种全局暂停现象,全局停顿,所有Java代码停止,native代码可以执行,但不能与JVM交互;这些现象多半是由于gc引起。

四、JVM调优中Java自带的常用的命令

1.jps:jps是java提供的一个显示当前所有java进程pid的命令

jps参数

C:\Users\a>jps --help
illegal argument: --help
usage: jps [-help]
       jps [-q] [-mlvV] [<hostid>]

Definitions:
    <hostid>:      <hostname>[:<port>]

jps –q:只显示pid,不显示class名称,jar文件名和传递给main方法的参数

jps –m:输出传递给main方法的参数,在嵌入式jvm上可能是null

jps –l:输出应用程序main class的完整package名或者应用程序的jar文件完整路径名

jps –v:输出传递给JVM的参数

jps –V:隐藏输出传递给JVM的参数

2.jstack: jstack能得到运行java程序的线程(java stack和native stack)的信息

jstack 参数

C:\Users\a>jstack --help
Usage:
    jstack [-l] <pid>
        (to connect to running process)
    jstack -F [-m] [-l] <pid>
        (to connect to a hung process)
    jstack [-m] [-l] <executable> <core>
        (to connect to a core file)
    jstack [-m] [-l] [server_id@]<remote server IP or hostname>
        (to connect to a remote debug server)

Options:
    -F  to force a thread dump. Use when jstack <pid> does not respond (process is hung)
    -m  to print both java and native frames (mixed mode)
    -l  long listing. Prints additional information about locks
    -h or -help to print this help message
-F  强制dump线程堆栈信息. 用于进程hung住, jstack <pid>命令没有响应的情况

-m  同时打印java和本地(native)线程栈信息,m是mixed mode的简写

-l 打印锁的额外信息

  1. jmap: jmap可以生成Java程序的堆的Dump文件,也可以查看堆内对象实例的统计信息,查看ClassLoader的信息以及finalizer队列

jmap参数

C:\Users\a>jmap --help
Usage:
    jmap [option] <pid>
        (to connect to running process)
    jmap [option] <executable <core>
        (to connect to a core file)
    jmap [option] [server_id@]<remote server IP or hostname>
        (to connect to remote debug server)

where <option> is one of:
    <none>               to print same info as Solaris pmap
    -heap                to print java heap summary
    -histo[:live]        to print histogram of java object heap; if the "live"
                         suboption is specified, only count live objects
    -clstats             to print class loader statistics
    -finalizerinfo       to print information on objects awaiting finalization
    -dump:<dump-options> to dump java heap in hprof binary format
                         dump-options:
                           live         dump only live objects; if not specified,
                                        all objects in the heap are dumped.
                           format=b     binary format
                           file=<file>  dump heap to <file>
                         Example: jmap -dump:live,format=b,file=heap.bin <pid>
    -F                   force. Use with -dump:<dump-options> <pid> or -histo
                         to force a heap dump or histogram when <pid> does not
                         respond. The "live" suboption is not supported
                         in this mode.
    -h | -help           to print this help message
    -J<flag>             to pass <flag> directly to the runtime system

clstats 打印类加载器统计信息

-dump: 即

-dump:[live,]format=b,file=

使用hprof二进制形式,输出jvm的heap内容到文件=. live子选项是可选的,假如指定live选项,那么只输出活的对象到文件.

-finalizerinfo 打印正等候回收的对象的信息.

-heap 打印heap的概要信息,GC使用的算法,heap的配置及wise heap的使用情况.

-histo[:live] 打印每个class的实例数目,内存占用,类全名信息. VM的内部类名字开头会加上前缀”*”. 如果live子参数加上后,只统计活的对象数量.

-permstat 打印classload和jvm heap长久层的信息. 包含每个classloader的名字,活泼性,地址,父classloader和加载的class数量. 另外,内部String的数量和占用内存数也会打印出来.

-F 强迫.在pid没有相应的时候使用-dump或者-histo参数. 在这个模式下,live子参数无效.

-h | -help 打印辅助信息

-J 传递参数给jmap启动的jvm

  1. jstat jstat命令可以查看堆内存各部分的使用量,以及加载类的数量。

jstat参数

C:\Users\a>jstat --help
invalid argument count
Usage: jstat -help|-options
       jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]

Definitions:
  <option>      An option reported by the -options option
  <vmid>        Virtual Machine Identifier. A vmid takes the following form:
                     <lvmid>[@<hostname>[:<port>]]
                Where <lvmid> is the local vm identifier for the target
                Java virtual machine, typically a process id; <hostname> is
                the name of the host running the target Java virtual machine;
                and <port> is the port number for the rmiregistry on the
                target host. See the jvmstat documentation for a more complete
                description of the Virtual Machine Identifier.
  <lines>       Number of samples between header lines.
  <interval>    Sampling interval. The following forms are allowed:
                    <n>["ms"|"s"]
                Where <n> is an integer and the suffix specifies the units as
                milliseconds("ms") or seconds("s"). The default units are "ms".
  <count>       Number of samples to take before terminating.
  -J<flag>      Pass <flag> directly to the runtime system.

options:由以下值构成

-class:显示ClassLoader的相关信息

-compiler:显示JIT编译的相关信息

-gc:显示与GC相关信息

-gccapacity:显示各个代的容量和使用情况

-gccause:显示垃圾收集相关信息(同-gcutil),同时显示最后一次或当前正在发生的垃圾收集的诱发原因

-gcnew:显示新生代信息

-gcnewcapacity:显示新生代大小和使用情况

-gcold:显示老年代信息

-gcoldcapacity:显示老年代大小

-gcpermcapacity:显示永久代大小

-gcutil:显示垃圾收集信息

-printcompilation:输出JIT编译的方法信息

-t:在输出信息前加上一个Timestamp列,显示程序的运行时间

-h:可以在周期性数据输出后,输出多少行数据后,跟着一个表头信息

interval:用于指定输出统计数据的周期,单位为毫秒

count:用于指定一个输出多少次数据

  1. jinfo:jinfo可以用来查看正在运行的java程序的扩展参数,甚至支持运行时,修改部分参数
C:\Users\a>jinfo --help
Usage:
    jinfo [option] <pid>
        (to connect to running process)
    jinfo [option] <executable <core>
        (to connect to a core file)
    jinfo [option] [server_id@]<remote server IP or hostname>
        (to connect to remote debug server)

where <option> is one of:
    -flag <name>         to print the value of the named VM flag
    -flag [+|-]<name>    to enable or disable the named VM flag
    -flag <name>=<value> to set the named VM flag to the given value
    -flags               to print VM flags
    -sysprops            to print Java system properties
    <no option>          to print both of the above
    -h | -help           to print this help message

-flag 以打印命名 VM 标志的值

-flag [+|-] 以启用或禁用命名的 VM 标志

-flag =将命名的 VM 标志设置为给定值

-flags 以打印虚拟机标志

-sysprops 打印 Java 系统属性

打印虚拟机标志和系统属性

  1. jvisualvm 可视化分析工具
```
常用参数配置
GC常用参数
-Xmn -Xms -Xmx -Xss
年轻代 最小堆 最大堆 栈空间

-XX:+UseTLAB
使用TLAB(线程独占缓冲区),默认打开

-XX:+PrintTLAB
打印TLAB的使用情况

-XX:TLABSize
设置TLAB大小

-XX:+DisableExplictGC
System.gc()不管用 ,FGC

-XX:+PrintGC

-XX:+PrintGCDetails

-XX:+PrintHeapAtGC

-XX:+PrintGCTimeStamps

-XX:+PrintGCApplicationConcurrentTime (重要性低)
打印应用程序时间

-XX:+PrintGCApplicationStoppedTime (重要性低)
打印暂停时长

-XX:+PrintReferenceGC (重要性低)
记录回收了多少种不同引用类型的引用

-verbose:class
类加载详细过程

-XX:+PrintVMOptions

在运行时,打印虚拟机接收到命令行显示参数

-XX:+PrintFlagsFinal -XX:+PrintFlagsInitial
查看初始默认值和修改后的参数

-Xloggc:opt/log/gc.log

输出GC日志的位置

-XX:MaxTenuringThreshold
升代年龄,最大值15

锁自旋次数 -XX:PreBlockSpin 热点代码检测参数-XX:CompileThreshold 逃逸分析 标量替换 …
这些不建议设置

Parallel常用参数
-XX:SurvivorRatio

survivor区比例

-XX:PreTenureSizeThreshold

大对象到底多大

-XX:MaxTenuringThreshold

-XX:+ParallelGCThreads
并行收集器的线程数,同样适用于CMS,一般设为和CPU核数相同

-XX:+UseAdaptiveSizePolicy
自动选择各区大小比例

-XX:MaxGCPauseMillis

垃圾收集器最大停顿的时间,但最大停顿时间过短必然会导致新生代的内存大小变小,垃圾回收频率变高,效率可能降低

-XX:GCTIMERatio

吞吐量大小(0-100),默认为99

CMS常用参数
-XX:+UseConcMarkSweepGC

-XX:ParallelCMSThreads
CMS线程数量

-XX:CMSInitiatingOccupancyFraction
使用多少比例的老年代后开始CMS收集,默认是68%(近似值),如果频繁发生SerialOld卡顿,应该调小,(频繁CMS回收)

-XX:+UseCMSCompactAtFullCollection
在FGC时进行压缩

-XX:CMSFullGCsBeforeCompaction
多少次FGC之后进行压缩(默认是0,即每次都压缩)

-XX:+CMSClassUnloadingEnabled

-XX:CMSInitiatingPermOccupancyFraction
达到什么比例时进行Perm回收

-XX:CMSPrecleaningEnabled

开启预清理(默认开启)

-XX:GCTimeRatio
设置GC时间占用程序运行时间的百分比

-XX:MaxGCPauseMillis
停顿时间,是一个建议时间,GC会尝试用各种手段达到这个时间,比如减小年轻代

更多参数参考:https://blog.csdn.net/zqz_zqz/article/details/70568819

G1常用参数
-XX:+UseG1Gc

-XX:G1HeapRegionSize=n

设置的G1区域的大小,值是2的幂,范围是1-32MB,目标是根据最小的java堆大小划分出约2048个区域;

随着size增加,垃圾的存活时间更长,GC间隔更长,但每次GC的时间也会更长。

-XX:MaxGCPauseMillis=n

最大GC停顿时间,这是个软目标,JVM将尽可能(但不保证)停顿小于这个时间(100)

-XX:InitiatingHeapOccupancyRercent=n

堆占用了多少的时候就触发GC,默认45

-XX:ConcGcThreads=n

并发GC使用的线程数

-XX:G1ReservePercent=n

设置作为空闲空间的预留内存百分比,以降低目标空间溢出的风险,默认10%
```
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值