生活随笔
收集整理的這篇文章主要介紹了
Jprofiler监控工具(内存泄漏)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
? ? 1、測試代碼
?
Java代碼 ?
public?class?JProfilerMemMain?{????????????private?List<Integer>?arr2?=?null;????????????public?void?test2()?{??????????arr2?=?test();??????}????????????public?List<Integer>?test()?{??????????List<Integer>?arr?=?new?ArrayList<Integer>();??????????for?(int?i?=?0;?i?<?200000;?i++)?{??????????????arr.add(i?*?100);??????????}??????????return?arr;??????}????????public?static?void?main(String[]?args)?throws?IOException?{??????????JProfilerMemMain?jp?=?new?JProfilerMemMain();??????????for?(int?i?=?1;?i?<=?10;?i++)?{??????????????jp.test2();??????????????????????}??????????System.out.println("程序執行完畢");??????????????????char?ch?=?'?';??????????while?(ch?!=?'n')?{??????????????ch?=?ch;??????????}??????}???????? ?
?
?
? 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代碼 ?
package?com.yyh.base.jprofile;????public?class?DeadlockMain??implements?Runnable?{??????boolean?flag;??????static?Object?o1?=?new?Object();??????static?Object?o2?=?new?Object();????????public?void?run()?{??????????System.out.println(flag);??????????if?(flag)?{??????????????synchronized?(o1)?{??????????????????try?{??????????????????????Thread.sleep(500);??????????????????}?catch?(InterruptedException?e)?{??????????????????????e.printStackTrace();??????????????????}??????????????????synchronized?(o2)?{??????????????????????System.out.println("AAA");??????????????????}??????????????}????????????}?else?{??????????????synchronized?(o2)?{??????????????????try?{??????????????????????Thread.sleep(500);??????????????????}?catch?(InterruptedException?e)?{??????????????????????e.printStackTrace();??????????????????}??????????????????synchronized?(o1)?{??????????????????????System.out.println("BBB");??????????????????}??????????????}????????????}????????}????????????public?static?void?main(String[]?args)?{??????????DeadlockMain?aaa?=?new?DeadlockMain();??????????DeadlockMain?bbb?=?new?DeadlockMain();??????????aaa.flag?=?true;??????????bbb.flag?=?false;??????????Thread?thA?=?new?Thread(aaa);??????????thA.setName("線程AAA");????????????????????Thread?thB?=?new?Thread(bbb);??????????thB.setName("線程BB");????????????????????thA.start();??????????thB.start();??????}??}?? ? ?
?
?2、查看步驟 ?
?
啟動JProfiler,等程序執行一段時間后查看如下,線程AAA和線程BB出現阻塞
?
?
查看Thread Views-> Thread? Monitor
?
①線程AAA的調用方的類和方法
②線程BB的調用方的類和方法
查看Thread?Views-> Thread ?Dumps
?
①線程AAA的阻塞的代碼位置
②線程BB的阻塞的代碼位置
?
轉載于:https://www.cnblogs.com/zhengah/p/5012976.html
總結
以上是生活随笔為你收集整理的Jprofiler监控工具(内存泄漏)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。