jprofiler Viewer memory leak

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:

Tomcat6,jdk6,JProfiler8

Memory leak phenomenon:

1. Perform batch operations some time in the server, find a memory instead of falling; even after gc, the memory can not be fully released;
2. Unless restart tomcat server memory is never freed, repeated these actions will result in no memory available, tomcat died;

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;

Performing a memory operation may cause leaks, for executing the memory recovery;

 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. 关闭内存记录:

点击“Stop Recordings”关闭内存记录,告诉jProfiler把这段记录作为分析对象;

5. 找到增加迅速的对象类型,打开HeapWalker:

在视图中找到增长快速的对象类型,一般包括String、char[]、Map等;

在这个操作中,String的增长速度很快。在Liver memory视图中找到String,点右键,选择“Show Selectiion In Heap Walker”,切换到HeapWarker 视图;

 

切换前会弹出选项页面,注意一定要选择“Select recorded  objects”;这样Heap Walker会在刚刚的那段记录中进行分析;否则,如果不勾选的话,他会分析tomcat的所有内存对象,这样既耗时又不准确;

 

6. 在HeapWalker中,找到泄漏的对象;

HeapWarker 会分析内存中的所有对象,包括对象的引用、创建、大小和数量;

 通过切换到References页签,可以看到具体的对象;

 

在这些内存对象中,找到泄漏的对象(应该被回收);可以在该对象上点击右键,选择“Use Selected Instances”缩小对象范围;

7. 通过引用分析该对象:

在References引用页签中,可以看到该对象的的引用关系,可以切换incoming/outcoming,显示引用的类型:

incoming  表示显示这个对象被谁引用;

outcoming 表示显示这个对象引用的其他对象;

 

选择“Show In Graph”将引用关系使用图形方式展现;

 

 

 

 

选中该对象,点击“Show Paths To GC Root”,会找到引用的根节点;

 

在上图中,我们可以发现,这个String对象最终的引用是在Thread线程中的ThreadLocalMap对象中;这给我们提供了线索,我们需要在程序中查找有关ThreadLocalMap部分的代码,检查为什么这个对象没有被释放;这往往就是泄漏的根源。

8. 通过创建分析该对象:

如果第7步还不能定位内存泄露的地方,我们可以尝试使用Allocations页签,该页签显示对象是如何创建出来的;
我们可以从创建方法开始检查,检查所有用到该对象的地方,直到找到泄漏位置;

 

内存泄漏的原因分析:

通过上面的分析,这次内存泄露主要由下面的原因造成:
有操作将对象存放在ThreadLocal的静态变量中,引用不会被释放,所以该对象不会被回收,一直存在于内存中,导致内存泄漏;

 

参考资料:

 1. 转载自: https://blog.csdn.net/coslay/article/details/43270311

 2.《利用Java剖析工具JProfiler查找内存泄漏的方法》http://jingyan.baidu.com/article/9c69d48f552d5f13c9024e3b.html

 

Guess you like

Origin www.cnblogs.com/caiba/p/12163364.html