Jprofiler监控工具(内存泄漏)

  • 内存泄漏

    1、测试代码

Java代码  收藏代码
  1. /** 
  2.  * JProfiler内存监控例子 
  3.  *  
  4.  * @author yhye 
  5.  * @2011-11-9上午09:46:06 
  6.  */  
  7. public class JProfilerMemMain {  
  8.       
  9.     private List<Integer> arr2 = null;  
  10.   
  11.     // 方法执行完后无法释放Integer的数据内存  
  12.     public void test2() {  
  13.         arr2 = test();  
  14.     }  
  15.   
  16.     // 方法执行完后释放Integer的数据内存  
  17.     public List<Integer> test() {  
  18.         List<Integer> arr = new ArrayList<Integer>();  
  19.         for (int i = 0; i < 200000; i++) {  
  20.             arr.add(i * 100);  
  21.         }  
  22.         return arr;  
  23.     }  
  24.   
  25.     public static void main(String[] args) throws IOException {  
  26.         JProfilerMemMain jp = new JProfilerMemMain();  
  27.         for (int i = 1; i <= 10; i++) {  
  28.             jp.test2();  
  29.             // jp.test();  
  30.         }  
  31.         System.out.println("程序执行完毕");  
  32.         // 以下方法为保持程序处于活动状态  
  33.         char ch = ' ';  
  34.         while (ch != 'n') {  
  35.             ch = ch;  
  36.         }  
  37.     }  
  38.       

  2、查看步骤  

    启动JProfiler,等程序打印出"程序执行完毕" 后查看如下,Integer对象有800264

    

   点击菜单上的按钮"Run GC" 执行垃圾回收后查看如下,Integer对象还有200270

   说明未完全释放数据,查看对象在堆的快照


从以下视图可以看到该对象的arr2属性有数据未释放

 ①Heap Walker->Biggest Objects


②Heap Walker->References,JProfilerMemMain对象自身占用空间16bytes,引用其他对象占空间4288kB

③Heap Walker->Data,中arr2属性有数据占用空间


  • 死锁

 1、测试代码

Java代码  收藏代码
  1. package com.yyh.base.jprofile;  
  2.   
  3. /** 
  4.  * 死锁例子 
  5.  * @author yhye 
  6.  * @2011-11-8上午09:12:25 
  7.  */  
  8. public class DeadlockMain  implements Runnable {  
  9.     boolean flag;  
  10.     static Object o1 = new Object();  
  11.     static Object o2 = new Object();  
  12.   
  13.     public void run() {  
  14.         System.out.println(flag);  
  15.         if (flag) {  
  16.             synchronized (o1) {  
  17.                 try {  
  18.                     Thread.sleep(500);  
  19.                 } catch (InterruptedException e) {  
  20.                     e.printStackTrace();  
  21.                 }  
  22.                 synchronized (o2) {  
  23.                     System.out.println("AAA");  
  24.                 }  
  25.             }  
  26.   
  27.         } else {  
  28.             synchronized (o2) {  
  29.                 try {  
  30.                     Thread.sleep(500);  
  31.                 } catch (InterruptedException e) {  
  32.                     e.printStackTrace();  
  33.                 }  
  34.                 synchronized (o1) {  
  35.                     System.out.println("BBB");  
  36.                 }  
  37.             }  
  38.   
  39.         }  
  40.   
  41.     }  
  42.       
  43.     public static void main(String[] args) {  
  44.         DeadlockMain aaa = new DeadlockMain();  
  45.         DeadlockMain bbb = new DeadlockMain();  
  46.         aaa.flag = true;  
  47.         bbb.flag = false;  
  48.         Thread thA = new Thread(aaa);  
  49.         thA.setName("线程AAA");  
  50.           
  51.         Thread thB = new Thread(bbb);  
  52.         thB.setName("线程BB");  
  53.           
  54.         thA.start();  
  55.         thB.start();  
  56.     }  
  57. }  

   

 2、查看步骤  

启动JProfiler,等程序执行一段时间后查看如下,线程AAA和线程BB出现阻塞


查看Thread Views-> Thread  Monitor

①线程AAA的调用方的类和方法


②线程BB的调用方的类和方法

查看Thread Views-> Thread  Dumps

①线程AAA的阻塞的代码位置

②线程BB的阻塞的代码位置


原文地址:https://www.cnblogs.com/zhengah/p/5012976.html