赞
踩
jstat(Java Virtual Machine Statistics Monitoring Tool) 用于监视虚拟机各种运行状态信息的命令行工具.可以显示虚拟机的类装载,内存,垃圾收集,JIT编译等运行数据.
远程命令模式下jstat是定位运行期JVM性能问题的首选工具.本地首选VisualVM.
使用方式可以参考官方文档.
jstat -gc <pid>: 可以显示gc的信息,查看gc的次数,及时间。
显示列名 | 具体描述 |
S0C | 年轻代中第一个survivor(幸存区)的容量 (字节) |
S1C | 年轻代中第二个survivor(幸存区)的容量 (字节) |
S0U | 年轻代中第一个survivor(幸存区)目前已使用空间 (字节) |
S1U | 年轻代中第二个survivor(幸存区)目前已使用空间 (字节) |
EC | 年轻代中Eden(伊甸园)的容量 (字节) |
EU | 年轻代中Eden(伊甸园)目前已使用空间 (字节) |
OC | Old代的容量 (字节) |
OU | Old代目前已使用空间 (字节) |
PC | Perm(持久代)的容量 (字节) |
PU | Perm(持久代)目前已使用空间 (字节) |
YGC | 从应用程序启动到采样时年轻代中gc次数 |
YGCT | 从应用程序启动到采样时年轻代中gc所用时间(s) |
FGC | 从应用程序启动到采样时old代(全gc)gc次数 |
FGCT | 从应用程序启动到采样时old代(全gc)gc所用时间(s) |
GCT | 从应用程序启动到采样时gc用的总时间(s) |
C:\Users\rongsoft>jstat -gc 2092
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
10752.0 10752.0 0.0 0.0 65536.0 29375.9 108032.0 21582.5 21248.0 20674.5 2560.0 2382.2 2 0.017 1 0.025
0.042
列成表格是像下面这样:
survivor0的容量 | survivor1的容量 | survivor0已用 | survivor1已用 | Eden容量 | Eden已用 | Old代容量 | Old代已用 | MetaSpace的容量 | MetaSpace已用 | 压缩类空间容量 | 压缩类空间使用大小 | 年轻代GC次数 | 年轻代GC所用时间 | 老年代GC次数 | 老年代GC所花时间 | GC所用总时间 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
S0C | S1C | S0U | S1U | EC | EU | OC | OU | MC | MU | CCSC | CCSU | YGC | YGCT | FGC | FGCT | GCT |
10752.0 | 10752.0 | 0.0 | 0.0 | 65536.0 | 29375.9 | 108032.0 | 21582.5 | 21248.0 | 20674.5 | 2560.0 | 2382.2 | 2 | 0.017 | 1 | 0.025 | 0.042 |
如果需要打印内存日志,需要增加参数:
-XX:+PrintGCDetails -XX:-UseAdaptiveSizePolicy -XX:SurvivorRatio=8 -XX:NewSize=10M -XX:MaxNewSize=10M
ps -ef |grep xxxxxx.jar
java -Xmx2048M -Xms512M -Djeesuite.configcenter.profile=pro -Djava.io.tmpdir=/home/server/microservice/xxxxx/temp/ -jar /home/server/microservice/xxxxx.jar
-XX:-UseAdaptiveSizePolicy 禁用动态调整,使SurvivorRatio可以起作用
-XX:SurvivorRatio=8 设置比例Eden:Survivior=8,那么2个Survivior区就是2.
jmap命令:可以查看堆默认配置信息
jmap -heap 995
- [root@service-docker-192-168-1-91.workflow]# jmap -heap 995
- Attaching to process ID 995, please wait...
- Debugger attached successfully.
- Server compiler detected.
- JVM version is 25.111-b14
-
- using thread-local object allocation.
- Parallel GC with 8 thread(s)
-
- Heap Configuration:
- MinHeapFreeRatio = 0
- MaxHeapFreeRatio = 100
- MaxHeapSize = 1073741824 (1024.0MB)
- NewSize = 178782208 (170.5MB)
- MaxNewSize = 357564416 (341.0MB)
- OldSize = 358088704 (341.5MB)
- NewRatio = 2
- SurvivorRatio = 8
- MetaspaceSize = 21807104 (20.796875MB)
- CompressedClassSpaceSize = 1073741824 (1024.0MB)
- MaxMetaspaceSize = 17592186044415 MB
- G1HeapRegionSize = 0 (0.0MB)
-
- Heap Usage:
- PS Young Generation
- Eden Space:
- capacity = 252182528 (240.5MB)
- used = 184114440 (175.58521270751953MB)
- free = 68068088 (64.91478729248047MB)
- 73.00840445219107% used
- From Space:
- capacity = 7864320 (7.5MB)
- used = 7864320 (7.5MB)
- free = 0 (0.0MB)
- 100.0% used
- To Space:
- capacity = 15728640 (15.0MB)
- used = 0 (0.0MB)
- free = 15728640 (15.0MB)
- 0.0% used
- PS Old Generation
- capacity = 358088704 (341.5MB)
- used = 317911272 (303.1838150024414MB)
- free = 40177432 (38.316184997558594MB)
- 88.7800336756783% used
-
- 34539 interned Strings occupying 4082072 bytes.
关于JVM得参数配置,可以参考官网https://docs.oracle.com/javase/9/gctuning/toc.htm
第四节 4 Factors Affecting Garbage Collection Performance
其中Virtual区是供新生代和老年代使用的.堆总大小为 Eden,survivor,survivor,Old.
Table 4-1 Default Options for 64-Bit Solaris Operating System
Option | Default Value |
---|---|
-XX:MinHeapFreeRatio | 40 |
-XX:MaxHeapFreeRatio | 70 |
-Xms | 6656 KB |
-Xmx | calculated |
After total available memory, the second most influential factor affecting garbage collection performance is the proportion of the heap dedicated to the young generation.
The bigger the young generation, the less often minor collections occur. However, for a bounded heap size, a larger young generation implies a smaller old generation, which will increase the frequency of major collections. The optimal choice depends on the lifetime distribution of the objects allocated by the application.
By default, the young generation size is controlled by the option -XX:NewRatio
.
For example, setting -XX:NewRatio=3
means that the ratio between the young and old generation is 1:3. In other words, the combined size of the eden and survivor spaces will be one-fourth of the total heap size.
The options -XX:NewSize
and -XX:MaxNewSize
bound the young generation size from below and above. Setting these to the same value fixes the young generation, just as setting -Xms
and -Xmx
to the same value fixes the total heap size. This is useful for tuning the young generation at a finer granularity than the integral multiples allowed by -XX:NewRatio
.
You can use the option -XX:SurvivorRatio
to tune the size of the survivor spaces, but often this isn't important for performance.
For example, -XX:SurvivorRatio=6
sets the ratio between eden and a survivor space to 1:6. In other words, each survivor space will be one-sixth of the size of eden, and thus one-eighth of the size of the young generation (not one-seventh, because there are two survivor spaces).
If survivor spaces are too small, then the copying collection overflows directly into the old generation. If survivor spaces are too large, then they are uselessly empty. At each garbage collection, the virtual machine chooses a threshold number, which is the number of times an object can be copied before it's old. This threshold is chosen to keep the survivors half full. You can use the log configuration -Xlog:gc,age
can be used to show this threshold and the ages of objects in the new generation. It's also useful for observing the lifetime distribution of an application.
Table 4-2 provides the default values for 64-bit Solaris.
Table 4-2 Default Option Values for Survivor Space Sizing
Option | Server JVM Default Value |
---|---|
| 2 |
| 1310 MB |
| not limited |
| 8 |
The maximum size of the young generation is calculated from the maximum size of the total heap and the value of the -XX:NewRatio
parameter. The "not limited" default value for the -XX:MaxNewSize
parameter means that the calculated value isn't limited by -XX:MaxNewSize
unless a value for -XX:MaxNewSize
is specified on the command line.
jstat查看容量大小:
jstat -gccapacity 31209
- [root@web-nginx-192-168-1-92.test92.ayg logs]# jstat -gccapacity 31209
- NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC
- 87040.0 174592.0 100352.0 2560.0 4608.0 79872.0 175104.0 349696.0 326656.0 326656.0 0.0 1165312.0 131624.0 0.0 1048576.0 14896.0 9779 12
jstat使用举例:详细可以参考jvm 性能调优工具之 jstat 命令详解
- [root@web-nginx-192-168-1-92 logs]# jps
- 9348 springcloud-apigateway.jar
- 18117 Bootstrap
- 5542 Bootstrap
- 3880 QuorumPeerMain
- 31209 mypayer.jar
- 5322 Bootstrap
- 4748 springcloud-admin.jar
- 5709 Bootstrap
- 4470 configserver.jar
- 7734 ConsoleConsumer
- 12822 Jps
- 20375 Bootstrap
- 8122 ConnectStandalone
- 4347 Kafka
- 4699 springcloud-eureka.jar
- You have new mail in /var/spool/mail/root
- [root@web-nginx-192-168-1-92 logs]# jstat -gccapacity 31209
- NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC
- 87040.0 174592.0 100352.0 2560.0 4608.0 79872.0 175104.0 349696.0 326656.0 326656.0 0.0 1165312.0 131624.0 0.0 1048576.0 14896.0 9779 12
- [root@web-nginx-192-168-1-92 logs]# jstat -gcold 31209
- MC MU CCSC CCSU OC OU YGC FGC FGCT GCT
- 131624.0 126963.1 14896.0 13916.6 326656.0 130010.1 9813 12 4.207 190.493
- [root@web-nginx-192-168-1-92 logs]# jstat -gcnew 31209
- S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT
- 3072.0 3072.0 2064.0 0.0 1 15 3072.0 168448.0 109317.2 14998 273.159
- You have new mail in /var/spool/mail/root
- [root@web-nginx-192-168-1-92 logs]# jstat -gcoldcapacity 31209
- OGCMN OGCMX OGC OC YGC FGC FGCT GCT
- 175104.0 349696.0 288768.0 288768.0 15002 17 6.059 279.273
- [root@web-nginx-192-168-1-92 logs]# jstat -gcnewcapacity 31209
- NGCMN NGCMX NGC S0CMX S0C S1CMX S1C ECMX EC YGC FGC
- 87040.0 174592.0 168448.0 57856.0 3072.0 57856.0 2048.0 173568.0 151552.0 15003 17
- [root@web-nginx-192-168-1-92 logs]# jstat -gcutil 31209
- S0 S1 E O M CCS YGC YGCT FGC FGCT GCT
- 99.22 0.00 11.10 56.16 96.39 93.46 15028 273.592 17 6.059 279.651
如果要打印GC日志,可以增加配置参数
- -XX:+PrintGC 输出GC日志
- -XX:+PrintGCDetails 输出GC的详细日志(jdk9已经废弃)
- -XX:+PrintGCTimeStamps 输出GC的时间戳(以基准时间的形式)
- -XX:+PrintGCDateStamps 输出GC的时间戳(jdk9已经移除.以日期的形式,如 2013-05-04T21:53:59.234+0800)
- -XX:+PrintHeapAtGC 在进行GC的前后打印出堆的信息
- -Xloggc:../logs/gc.log 日志文件的输出路径
注意:已经废弃的参数配置后可能导致启动失败.
jmap导出文件
jmap -dump:format=b,live,file=jmap.dump 31209
参考:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。