一、測試代碼測試
- 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;
- }
- }
-
二、查看步驟 spa
啓動JProfiler,等程序打印出"程序執行完畢" 後查看以下,Integer對象有800264個線程
點擊菜單上的按鈕"Run GC" 執行垃圾回收後查看以下,Integer對象還有200270個orm
說明未徹底釋放數據,查看對象在堆的快照對象
從如下視圖能夠看到該對象的arr2屬性有數據未釋放內存
①Heap Walker->Biggest Objectsstring
②Heap Walker->References,JProfilerMemMain對象自身佔用空間16bytes,引用其餘對象佔空間4288kBit
③Heap Walker->Data,中arr2屬性有數據佔用空間io
一、測試代碼class
- 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();
- }
- }
二、查看步驟
啓動JProfiler,等程序執行一段時間後查看以下,線程AAA和線程BB出現阻塞
查看Thread Views-> Thread Monitor
①線程AAA的調用方的類和方法
②線程BB的調用方的類和方法
查看Thread Views-> Thread Dumps
①線程AAA的阻塞的代碼位置
②線程BB的阻塞的代碼位置