In a recent work by JProfiler solve the problem of a memory leak, will now step of detecting and recorded some analysts, may have been used as reference for future encountered similar problems.
Operating environment:
Memory leak phenomenon:
Use check for memory leaks steps JProfiler:
1. Initialize the test environment:
Switch to the "Live Memory -> All Objects" tab, you can see the object in the current tomcat.
Before performing the operation, you need to run the "Run GC", the jvm for memory recovery, clean up invalid object;
In order to facilitate growth of the comparison memory, click "Mark Current" button, to the current memory usage as a reference;
It is displayed after clicking "Difference" column, which lists the number of objects changes and rate of change;
2. Open the memory record:
Click "Start Recordings" button to start recording. The main purpose of the implementation of this step is to following "Heap Walker", set up a monitoring interval; if you do not record the words, "Heap Walker" will analyze all memory jvm virtual machine that is time-consuming and may not accurately find the cause of memory leaks.
3. Perform the operations performed GC;
You can click on the "update" button to update the view;
After performing garbage collection target, still exists in memory there may be a leak of the object; in the following figure, instance count in the red sector object can not be recycled, difference column lists the number of objects increase and growth; for example a String , the increase in the operation target 31751, an increase of 14%, these objects will then be observed in HeapWalker, the analysis of which objects are leaking;
Objects are generally caused by leakage include: String, char [], HashMap, these objects need to focus on the next;
4. 关闭内存记录:
5. 找到增加迅速的对象类型,打开HeapWalker:
在视图中找到增长快速的对象类型,一般包括String、char[]、Map等;
切换前会弹出选项页面,注意一定要选择“Select recorded objects”;这样Heap Walker会在刚刚的那段记录中进行分析;否则,如果不勾选的话,他会分析tomcat的所有内存对象,这样既耗时又不准确;
6. 在HeapWalker中,找到泄漏的对象;
通过切换到References页签,可以看到具体的对象;
在这些内存对象中,找到泄漏的对象(应该被回收);可以在该对象上点击右键,选择“Use Selected Instances”缩小对象范围;
7. 通过引用分析该对象:
incoming 表示显示这个对象被谁引用;
outcoming 表示显示这个对象引用的其他对象;
选择“Show In Graph”将引用关系使用图形方式展现;
选中该对象,点击“Show Paths To GC Root”,会找到引用的根节点;
在上图中,我们可以发现,这个String对象最终的引用是在Thread线程中的ThreadLocalMap对象中;这给我们提供了线索,我们需要在程序中查找有关ThreadLocalMap部分的代码,检查为什么这个对象没有被释放;这往往就是泄漏的根源。
8. 通过创建分析该对象:
内存泄漏的原因分析:
参考资料:
1. 转载自: https://blog.csdn.net/coslay/article/details/43270311
2.《利用Java剖析工具JProfiler查找内存泄漏的方法》http://jingyan.baidu.com/article/9c69d48f552d5f13c9024e3b.html