泥土巢 - JVM性能调优
https://www.nituchao.com/category/jvm-tuning/
JVM性能调优相关的技术探讨。JVM配置参数-X与-XX的区别
https://www.nituchao.com/jvm-tuning/8.html
2017-06-13T15:52:00+08:00启动JVM时通过指定配置参数来指导虚拟机按照我们的要求提供服务,这一点对大多数的Java程序员来说已经是司空见惯。在指定配置参数时,会有-X和-XX两种形式,那么它们两者有什么区别呢,今天我想借这篇文章总结一下。下面是我们的某个Java项目在正式环境上启动JVM时的一个典型命令,在该命令中指定了各种启动参数:java -Xmx15G \
-Xms10G \
-Xmn3G \
-Xss512k \
-XX:MaxPermSize=512M \
-XX:PermSize=512M \
-XX:+PrintFlagsFinal \
-XX:MaxTenuringThreshold=1 \
-XX:SurvivorRatio=23 \
-XX:TargetSurvivorRatio=80 \
-Xnoclassgc \
-XX:+UseParNewGC \
-XX:+UseConcMarkSweepGC \
-XX:CMSInitiatingOccupancyFraction=80 \
-XX:ParallelGCThreads=24 \
-XX:ConcGCThreads=24 \
-XX:+CMSParallelRemarkEnabled \
-XX:+CMSScavengeBeforeRemark \
-XX:+ExplicitGCInvokesConcurrent \
-XX:+UseTLAB \
-XX:TLABSize=64K, -verbose:gc \
-XX:+PrintGCDetails \
-XX:+PrintGCDateStamps \
-XX:+PrintGCTimeStamps \
-XX:+PrintGCApplicationStoppedTime \
-Xloggc:./gc.logJava HotSpot VM的官方文档中将启动参数分为如下两类:配置 参数类型说明举例-Xnon-standard非标准参数。<br/><br/>这些参数不是虚拟机规范规定的。因此,不是所有VM的实现(如:HotSpot,JRockit,J9等)都支持这些配置参数。-Xmx、-Xms、-Xmn、-Xss-XXnot-stable不稳定参数。<br/><br/>这些参数是虚拟机规范中规定的。这些参数指定虚拟机实例在运行时的各种行为,从而对虚拟机的运行时性能有很大影响。-XX:SurvivorRatio、-XX:+UseParNewGc补充: -X和-XX两种参数都可能随着JDK版本的变更而发生变化,有些参数可以能会被废弃掉,有些参数的功能会发生改变,但是JDK官方不会通知开发者这些变化,需要使用者注意。-XX参数被称为不稳定参数,是因为这类参数的设置会引起JVM运行时性能上的差异,配置得当可以提高JVM性能,配置不当则会使JVM出现各种问题, 甚至造成JVM崩溃。国外有个哥们从HotSpot VM的源码里发现了934个此类型的配置参数,因此能对JVM做出很多组合配置,对JVM的调优也没有统一的标准,需要我们在实践中不断总结经验,并结合实际业务来进行操作,最终找到最适合当前业务的那些配置。一些有用的-XX配置对于-XX类型的配置选项,虚拟机规范有一些惯例,针对不同的平台虚拟机也会提供不同的默认值。对于布尔(Boolean)类型的配置选项,通过-XX:+<option>来开启,通过-XX:-<option>来关闭。对于数字(Numberic)类型的配置选项,通过-XX:<option>=<number>来配置。<number>后面可以携带单位字母,比如: 'k'或者'K'代表千字节,'m'或者'M'代表兆字节,'g'或者'G'代表千兆字节。对于字符串(String)类型的配置选项,通过-XX:<option>=<string>来配置。这种配置通过用来指定文件,路径或者命令列表。参考文献1, JVM Options - The complete reference2, Java HotSpot VM OptionsYourKit远程连接线上服务器
https://www.nituchao.com/jvm-tuning/7.html
2017-06-06T15:51:00+08:00YourKit是一款业内领先的性能分析工具,目前支持Java和.NET两个平台。该工具功能全面强悍,能通过本地连接或者远程连接的方式,对各种服务器,框架,平台的性能进行分析,并提供了多种由浅入深,针对开发环境或者生产环境的分析模式。该工具提供了高效的图形化显示方式,动动鼠标就可以对系统进行显微镜式的观察分析。通过YourKit可以对以下内容进行分析:CPU profiling - investigate performace issuesMemory profiling - memory leaks, usage, GCThreads and synchronizationException profilingWeb, Database, I/O本文想总结一下,YourKit提供的两种连接到Java进程的方式,一种是attach方式,另一种是integrate方式。通过attach方式连接到远程服务器在控制台,attach方式可以通过进程号,连接到运行中的任何Java进程中,这种方式并不保证总能连接成功,而且会禁用某些分析功能。线上环境CentOS release 6.3 64-BitJava 1.7.0_79 HotSpot(TM) 64-Bit Server VM下载安装包# wget https://www.yourkit.com/download/yjp-2017.02-b53.zip
# unzip yjp-2017.02-b53.zip
# cd yjp-2017.02确定服务进程号# jps
2230 Resin
3959 JpsConsole连接进程通过下面的命令来连接到Java进程。# bin/yjp.sh -attach
[YourKit Java Profiler 2017.02-b53] Log file: /root/.yjp/log/yjp-4473.log
Running JVMs:
Name | PID| Status
-------------------------------- |------|--------------------------------
Resin | 11760| Ready for attach
ThriftMain | 2934| Ready for attach
Resin | 2230| Agent already loaded, agent port is 10001
Resin | 14232| Ready for attach
Resin | 3657| Ready for attach
WatchdogManager | 16411| Ready for attach
Enter PID of the application you want to attach (0 to exit) and press Enter:
>2230
Please specify comma-separated list of startup options, or press Enter for default options (recommended):
>上面的操作执行完成后,会出现如下提示,表示YourKit Console服务已经成功运行,并在10001开始工作。Attaching to process 2230 using default options
[YourKit Java Profiler 2017.02-b53] Log file: /root/.yjp/log/yjp-30209.log
The profiler agent has attached. Waiting while it initializes...
The agent is loaded and is listening on port 10001.
You can connect to it from the profiler UI.
# lsof -i:10001
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 2230 root 72u IPv4 199000044 0t0 TCP *:scp-config (LISTEN)在浏览器,通过ip:port的方式,可以看到YourKit的运行概况。YourKit UI连接远程服务打开YourKit UI,点击Connect to remote application,输入IP:PORT进行连接。连接成功后,进行性能分析界面,大功告成。疑难问题通过attach方式并不总能连接到Java进程,常常会出现JVM无响应的问题,控制台报错如下:Attaching to process 31833 using options
com.yourkit.util.bf: com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded
at com.yourkit.b.f.a(a:128)
at com.yourkit.b.c.attach(a:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.yourkit.h.run(a:17)
Attach to a running JVM failed.
Solution: start JVM with the profiler agent instead of attaching it to a running JVM:
https://www.yourkit.com/docs/java/help/running_with_profiler.jsp因此,分析本地Java进程时可以选择attach方式,对于远程服务,还是推荐integrate方式。通过integrate方式连接到远程服务器(重点推荐)integrate方式通过修服务器启动配置文件,随服务启动,这种方式比较稳定,而且能够全面启用YourKit的所有功能。线上环境CentOS release 6.3 64-BitJava 1.7.0_79 HotSpot(TM) 64-Bit Server VM下载安装包# wget https://www.yourkit.com/download/yjp-2017.02-b53.zip
# unzip yjp-2017.02-b53.zip
# cd yjp-2017.02确定服务进程号# jps
2230 Resin
3959 JpsConsole连接进程通过下面的命令来连接到Java进程。# bin/yjp.sh -integrate
Choose server to integrate with:
1) Geronimo
2) GlassFish
3) JBoss / WildFly
4) Jetty
5) JRun 4
6) Resin 3.1/4
7) Tomcat 3–8
8) WebLogic 9 and newer
9) WebSphere Application Server 7 or newer
10) WebSphere Application Server V8.5 Liberty profile
11) Generic server (use if your server is not on the list)
Enter number which corresponds to your server (0 to exit) and press Enter:
>6
Please specify whether the server runs on a 32-bit JVM or a 64-bit JVM.
Hint: If you are not sure what to choose, choose "32-bit JVM". If with this choice the server does not start with profiling, re-run the integration and choose "64-bit Java" option.
1) 32-bit JVM
2) 64-bit JVM
>2
Resin configuration file (<RESIN_HOME>/conf/resin.xml or <RESIN_HOME>/conf/resin.conf):
>/home/work/bin/miui-sec-adv/resin/conf/resin.xml
Startup options configuration: step 1 of 5
Should option 'disablestacktelemetry' be specified?
1) Yes (recommended to minimize profiling overhead in production)
2) No
>2
Startup options configuration: step 2 of 5
Should option 'exceptions=disable' be specified?
1) Yes (recommended to minimize profiling overhead in production)
2) No
>1
Startup options configuration: step 3 of 5
Built-in probes:
1) Enabled: recommended for use in DEVELOPMENT; gives high level profiling results, but may add overhead
2) Disabled: recommended for use in PRODUCTION to minimise overhead, or for troubleshooting
Hint: It's recommended to choose #1 in development and #2 in production.
If choosing #1 makes profiling overhead big or there are startup issues, re-run the integration and choose #2.
>2
Startup options configuration: step 4 of 5
Should option 'delay=10000' be specified?
1) Yes (recommended)
2) No
>1
Startup options configuration: step 5 of 5
Please specify comma-separated list of additional startup options, or press Enter for no additional options:
>
A new config file is created:
/home/work/bin/miui-sec-adv/resin/conf/resin_yjp.xml
To profile the server:
1) [Recommended] Backup original 'resin.xml'
2) Rename 'resin_yjp.xml' to 'resin.xml'
3) Start the server上面的操作执行完成后,会在操作中指定的resin配置文件目录生成resin_yjp.xml文件,接下来将该resin_yjp.xml重命名为resin.xml,并启动服务器。正常情况下,YourKit的Console程序便会随着Resin服务器自动启动,可以通过lsof命令确认一下。# lsof -i:10001
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 2389 work 8u IPv4 3188835745 0t0 TCP *:scp-config (LISTEN)同时,可以通过IP:PORT的方式在浏览器中查看YourKit Console服务的概况。YourKit UI连接远程服务打开YourKit UI,点击Connect to remote application,输入IP:PORT进行连接。连接成功后,进行性能分析界面,大功告成。