java---不停机控制jvm的gc选项

来源:互联网 发布:哪里有淘宝店铺转让 编辑:程序博客网 时间:2024/05/20 21:22

背景

很久之前的事情了,在很久很久……
一般在做性能测试的时候为最小限度影响性能,基本会不开gc日志,但出现问题,有要查看相关信息,麻勒个烦啊

有没有两全其美的办法?

废话略过,往下看

jinfo

jinfo(Java Configuration Information),主要用于查看正在运行的Java进程(或核心文件、远程调试服务器)的Java配置信息
用法

[root@localhost config]# jinfoUsage:    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选项(圈起来要考试~)
在使用-flag选项时,jinfo实用程序可以动态调整特定Java进程的特定Java VM标志的值。这类选项的列表是有限的,但在一些场合仍然有用。JVM上的这些标志的完整列表可以通过以下命令进行检查

[root@localhost config]# java -XX:+PrintFlagsFinal -version|grep manageable     intx CMSAbortablePrecleanWaitMillis            = 100                                 {manageable}     intx CMSTriggerInterval                        = -1                                  {manageable}     intx CMSWaitDuration                           = 2000                                {manageable}     bool HeapDumpAfterFullGC                       = false                               {manageable}     bool HeapDumpBeforeFullGC                      = false                               {manageable}     bool HeapDumpOnOutOfMemoryError                = false                               {manageable}    ccstr HeapDumpPath                              =                                     {manageable}    uintx MaxHeapFreeRatio                          = 100                                 {manageable}    uintx MinHeapFreeRatio                          = 0                                   {manageable}     bool PrintClassHistogram                       = false                               {manageable}     bool PrintClassHistogramAfterFullGC            = false                               {manageable}     bool PrintClassHistogramBeforeFullGC           = false                               {manageable}     bool PrintConcurrentLocks                      = false                               {manageable}     bool PrintGC                                   = false                               {manageable}     bool PrintGCDateStamps                         = false                               {manageable}     bool PrintGCDetails                            = false                               {manageable}     bool PrintGCID                                 = false                               {manageable}     bool PrintGCTimeStamps                         = false                               {manageable}

-XX:+PrintFlagsFinal列出了所有的JVM选项,其中的 “manageable” 选项目前是有兴趣的。通过JDK管理界面可以动态的写(com.sun.management.HotSpotDiagnosticMXBean API)。

应用

 -flag [+|-]<name>    to enable or disable the named VM flag

从参数的解释来看很直白,启用(+)或者关闭(-)vm标识

一个未开启gclog的java应用,不停机打开gclog,然后再关闭gclog

[root@localhost config]# jps31519 main20605 Jps[root@localhost config]# 

使用jinfo查看启动信息

[root@localhost bin]#jinfo 31519……VM Flags:Non-default VM flags: -XX:CICompilerCount=3 -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSParallelRemarkEnabled -XX:+DisableExplicitGC -XX:InitialHeapSize=2147483648 -XX:LargePageSizeInBytes=134217728 -XX:MaxHeapSize=2147483648 -XX:MaxNewSize=268435456 -XX:MaxTenuringThreshold=6 -XX:MinHeapDeltaBytes=196608 -XX:NewSize=268435456 -XX:OldPLABSize=16 -XX:OldSize=1879048192 -XX:+PrintGC -XX:+PrintGCTimeStamps -XX:ThreadStackSize=256 -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseConcMarkSweepGC -XX:+UseFastAccessorMethods -XX:+UseFastUnorderedTimeStamps -XX:+UseParNewGC Command line:  -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Xmx2g -Xms2g -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -Xloggc:/root/gc.log -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70
[root@localhost config]# jinfo -flag +PrintGCDetails -flag +PrintGC 31519[root@localhost config]# jinfo -flag +PrintGC 31519[root@localhost config]#

新启窗口查看日志

[root@localhost bin]#tail -f ~/gc.log146.851: [GC (Allocation Failure) 146.851: [ParNew: 232060K->17317K(235968K), 0.0819086 secs] 232060K->27019K(2070976K), 0.0820622 secs] [Times: user=0.26 sys=0.02, real=0.08 secs] 158.541: [GC (Allocation Failure) 158.541: [ParNew: 227109K->7673K(235968K), 0.0315112 secs] 236811K->25629K(2070976K), 0.0316241 secs] [Times: user=0.12 sys=0.01, real=0.03 secs] 166.687: [GC (Allocation Failure) 166.687: [ParNew: 217465K->2602K(235968K), 0.0163507 secs] 235421K->20558K(2070976K), 0.0164941 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 173.471: [GC (Allocation Failure) 173.471: [ParNew: 212394K->2816K(235968K), 0.0111496 secs] 230350K->20772K(2070976K), 0.0112630 secs] [Times: user=0.04 sys=0.00, real=0.01 secs] 179.828: [GC (Allocation Failure) 179.828: [ParNew: 212608K->3739K(235968K), 0.0164724 secs] 230564K->21695K(2070976K), 0.0166167 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 186.226: [GC (Allocation Failure) 186.226: [ParNew: 213531K->4628K(235968K), 0.0242745 secs] 231487K->22584K(2070976K), 0.0244269 secs] [Times: user=0.09 sys=0.00, real=0.02 secs] 192.607: [GC (Allocation Failure) 192.607: [ParNew: 214420K->4752K(235968K), 0.0170171 secs] 232376K->22708K(2070976K), 0.0171690 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 

执行命令

[root@localhost ~]# jinfo -flag +PrintGCTimeStamps 31519[root@localhost ~]# jinfo -flag +PrintGCDateStamps 31519

继续查看日志

……231.252: [GC (Allocation Failure) 231.252: [ParNew: 210933K->699K(235968K), 0.0160253 secs] 231484K->21606K(2070976K), 0.0161905 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 237.841: [GC (Allocation Failure) 237.841: [ParNew: 210491K->562K(235968K), 0.0155473 secs] 231398K->21472K(2070976K), 0.0157101 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 2017-10-12T17:31:05.648+0800: 244.442: [GC (Allocation Failure) 2017-10-12T17:31:05.648+0800: 244.442: [ParNew: 210354K->443K(235968K), 0.0156593 secs] 231264K->21353K(2070976K), 0.0158283 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 2017-10-12T17:31:12.142+0800: 250.935: [GC (Allocation Failure) 2017-10-12T17:31:12.142+0800: 250.936: [ParNew: 210235K->655K(235968K), 0.0154720 secs] 231145K->21608K(2070976K), 0.0156407 secs] [Times: user=0.05 sys=0.00, real=0.01 secs] 2017-10-12T17:31:18.286+0800: 257.079: [GC (Allocation Failure) 2017-10-12T17:31:18.286+0800: 257.079: [ParNew: 210447K->782K(235968K), 0.0152549 secs] 231400K->21735K(2070976K), 0.0154234 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] 2017-10-12T17:31:24.674+0800: 263.468: [GC (Allocation Failure) 2017-10-12T17:31:24.674+0800: 263.468: [ParNew: 210574K->938K(235968K), 0.0162392 secs] 231527K->21917K(2070976K), 0.0164128 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 

再依次执行命令(每条命名间隔一会)

[root@localhost ~]# jinfo -flag -PrintGCDateStamps 31519[root@localhost ~]# jinfo -flag -PrintGCTimeStamps 31519[root@localhost ~]# jinfo -flag -PrintGCDetails 31519[root@localhost ~]# jinfo -flag -PrintGC 31519

继续查看日志文件

2017-10-12T17:31:24.674+0800: 263.468: [GC (Allocation Failure) 2017-10-12T17:31:24.674+0800: 263.468: [ParNew: 210574K->938K(235968K), 0.0162392 secs] 231527K->21917K(2070976K), 0.0164128 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] 269.794: [GC (Allocation Failure) 269.794: [ParNew: 210730K->1423K(235968K), 0.0164091 secs] 231709K->22402K(2070976K), 0.0165705 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 276.287: [GC (Allocation Failure) 276.287: [ParNew: 211215K->1530K(235968K), 0.0163695 secs] 232194K->22510K(2070976K), 0.0165236 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] [GC (Allocation Failure) [ParNew: 211322K->1758K(235968K), 0.0165157 secs] 232302K->22737K(2070976K), 0.0166601 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] [GC (Allocation Failure) [ParNew: 211550K->1837K(235968K), 0.0160382 secs] 232529K->22961K(2070976K), 0.0161843 secs] [Times: user=0.06 sys=0.00, real=0.02 secs] [GC (Allocation Failure) [ParNew: 211629K->1928K(235968K), 0.0163444 secs] 232753K->23154K(2070976K), 0.0164922 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] [GC (Allocation Failure) [ParNew: 211720K->1640K(235968K), 0.0164974 secs] 232946K->23103K(2070976K), 0.0166554 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] [GC (Allocation Failure) [ParNew: 211432K->2359K(235968K), 0.0164740 secs] 232895K->23957K(2070976K), 0.0166221 secs] [Times: user=0.05 sys=0.00, real=0.02 secs] [GC (Allocation Failure)  233749K->23418K(2070976K), 0.0170608 secs][GC (Allocation Failure)  233210K->23428K(2070976K), 0.0164514 secs][GC (Allocation Failure)  233220K->23703K(2070976K), 0.0164186 secs]不再输出日志 no more

管不管用自己动手搞一下就知道了~~

原文连接 http://blog.csdn.net/yue530tomtom/article/details/78217688

原创粉丝点击