Java Application Tuning Guide - Tools

Reprinted from: http://calvin1978.blogcn.com/articles/perf-tunning-2.html?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io

 

The tuning of Java applications, if you don't write it, you will forget it all. You should deal with it first. The free JMC is really easy to use. The sooner you use it, the better.

The previous one is  another Java application tuning guide from three months ago - appetizers

 

1. Two major pieces of soil method tuning

First of all, when people don’t have Profile tools, the two most important things for tuning are Heap Dump and Thread Dump.

 

1.1 Heap Dump

 

jmap -dump:live,format=b,file=heap.hprof pid

From the security point log, starting from the Heap Dump, the entire JVM is paused. Considering the IO (although it is written to the Page Cache, it may encounter a background flush), a few gigabytes of Heap may pause for a few seconds. Be cautious when executing on a production environment.

The live option actually generates a Full GC to ensure that only surviving objects are seen. Sometimes, the live option is deliberately not added to see historical objects.

It is recommended to open the file from Dump with the VisualVM or Eclipse MAT plug-in that comes with the JDK. There are two ways to count the size of the object:

  • Shallow Size: The original size of the object.
  • Retained Size: The current object size + the sum of the sizes of objects directly or indirectly referenced by the current object.

When looking at the size of itself, the main ones are char[], byte[] and the like, which is meaningless (using jmap -histo:live pid to see its own size). So what you need to care about is to keep objects with larger sizes and see who is referencing these char[], byte[].

(MAT can see more information, but VisualVM is better than the built-in JVM. The usage is as follows: enter jvisualvm on the command line, file->load->heap Dump->check->find 20 objects with the largest reserved size, it will trigger The calculation of the reserved size, then you can browse in the class view and sort by the reserved size)
 

1.2 Thread Dump

ThreadDump can also cause JVM stalls and should be executed with caution on production systems.

It can be executed from the command line, "jstack pid" or "jstack -l pid", where -l will print various locks at the same time, but will make the JVM pause much longer (may be many times worse, such as ordinary jstack may There is no difference between a few milliseconds and a GC, adding -l is nearly one second), -l is not recommended.

The other is to print directly with the code. For example, when the thread pool is full, new tasks cannot be added. In the development or performance test mode, you can directly print the current thread pool situation after catching the exception in the code.

 

ThreadMXBean threadMBean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfos = threadMBean.dumpAllThreads(false, false);

Also note that the parameter of threadMBean.dumpAllThreads(false, false) is false, and if the parameter is changed to true, the synchronizers and monitors will be printed, which also makes the JVM pause for a long time.

thread status:

  • RUNNABLE: In the running state, the word "locked" may still be seen in it, indicating that it has obtained a lock.
  • BLOCKED: Blocked by a lock (synchronizers).
  • WAITING: Waiting for a condition or monitor to occur, generally staying in statements such as park(), wait(), sleep(), join(), etc.
  • TIME_WAITING:和WAITING的区别是wait() 等语句加上了时间限制 wait(timeout)。

分析工具:

 

2. 你真正要的Java Mission Control

如果你使用过JProfiler,Yourkit,VisualVM还有Eclipse的Profiler插件等一堆Profiler工具,或者用JavaSimion等在代码里打印过metrics,最后会发现免费的JMC才是你想要的。
 

2.1 优点

代替收费的JProfiler的好东西,以前Bea JRockit的宝贝,现在随着JDK7 up40以后的版本免费自带了,不过只在开发环境免费,就是说理论上不能拿它来连生产环境的机器。

另一个让人开心的事情就是JMC采用采样,而不是传统的代码植入的技术,对应用性能的影响非常非常小,完全可以开着JMC来做压测(唯一影响可能是full gc多了,减少一些监控项看看)。不会像以前,开了代码植入型的Profiler,出来的性能测试结果差了一个数量级不说,热点完全可能是错误的,这是一个真实的故事,具体细节就不说了。
 

2.2 功能

JMC里可以看的东西太多了,自己觉得最有用的如下:

  • 内存Tab:分配Tab里的按类、按线程、对象的创建调用栈来归类的对象创建情况,让对象创建无处躲藏。
  • 内存Tab:GC Tab的GC详细情况,以及TLAB外的分配情况(每条线程在Heap里分了一个Thread Local Area,在TLAB里的内存分配不需要线程竞争,所以TLAB之外的分配是不好的)
  • 代码Tab:热点方法类及它的调用栈,超有用的功能。调用树是从线程角度看的方法调用,而按包名分类可以看3PP包的问题。
  • 线程Tab:热点线程,再换个姿势来看热点方法和调用树。
  • 线程Tab:争用,等待时间,锁定实例等。

 

2.3 使用方法简述

JDK7在启动服务时加上-XX:+UnlockCommercialFeatures -XX:+FlightRecorder 。
如果是远程服务器,要开JMX:

 

“-Dcom.sun.management.jmxremote.port=7001 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=your ip”

JDK自带的jmc命令,文件->连接->设定JMX连接,启动飞行纪录,固定时间选1分钟或更多,事件设置选为profiling,然后进一步修改,自己查看下都Profile了哪些信息,觉得不够的再添加些(下次就直接用上次设定就好了),比如:

  • 加上对象数量的统计:Java Virtual Machine->GC->Detail->Object Count/Object Count after GC
  • 方法调用采样的间隔从10ms改为1ms(但不能低于1ms,否则会影响性能了): Java Virtual Machine->Profiling下的两个选项
  • Socket与File采样, 10ms太久,但即使改为1ms也未必能抓住什么,可以干脆取消掉: Java Application->File Read/FileWrite/Socket Read/Socket Write

然后就开始Profile,到时间后Profile结束,会自动把记录下载回来,在JMC中展示。

其他资料:

 

3. BTrace

神器,在生产环境上,动态监控某些方法的执行时长及其他信息,不再需要自己手工打日志,发版本,部署与重启服务。

据说淘宝就是经常开着BTrace在线上找问题的,我们最近也在生产上试了几把,太爽利了。

使用方法网上一搜大把,就不重复了。

原理就是自己写一个类似AspectJ的,希望监控哪个方法,监控后做什么动作的脚本,然后动态执行btrace命令将这个脚本attach到某个JVM上就行

 
这是一篇严肃的文章,就不配图了。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326123597&siteId=291194637