---
title: "使用jprofiiler远程监控tomcat【入门使用篇】"
author: "chenchendefeng"
date: "2009-08-23T13:26:16Z"
tags: []
slug: "jprofiiler-tomcat"
draft: false
lastmod: "2009-08-23T13:26:16Z"
id: 455779
url: "https://chenchendefeng.iteye.com/blog/455779"
---

使用JProfiler剖析Java Application中对象的分配和回收（gc）

2008年09月02日 星期二 13:01

JProfiler是一款Java的性能监控工具。可以查看当前应用的对象、对象引用、内存、CPU使用情况、线程、线程运行情况（阻塞、等待等），同时可以查找应用内存使用得热点，即：哪个对象占用的内存比较多；或者CPU热点，即：哪儿方法占用的较大的CPU资源。

下面结合一个简单的例子说明Jprofiler如何使用

> package my;
> 
> import java.io.IOException;  
> import java.util.ArrayList;
> 
> public class Test {
> 
>     public static void main(String\[] args) throws IOException {  
>        char ch=' ';  
>         while(ch!='n')  
>             ch=(char)System.in.read();  
>         for(int i=0;i&lt;1000;i++)  
>             test();  
>         ch=' ';  
>         while(ch!='n')  
>             ch=(char)System.in.read();  
>     }
> 
>     public static void test(){  
>         ArrayList&lt;Integer&gt; arr=new ArrayList&lt;Integer&gt;();  
>         for(int i=0;i&lt;10000;i++)  
>             arr.add(new Integer((int)(Math.random()\*100)));  
>         //arr.clear();  
>         //arr=null;  
>     }  
> }

代码中红色的部分是为了停下来对JProfiler进行操作和观察结果而设置的。

经过实验，发现蓝色部分加上和不加结果是一样的。因为arr是局部变量，test()运行结束时其生命周期也结束了，arr所引用的对象也被回收。因此也没必要设置为null。

1、启动JProfiler，选择local application

[![1](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/1-thumb.png)](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/1.png)

2、进入下一步，设置参数

[![2](http://image-003.yo2cdn.com/wp-content/uploads/233/23380/2008/05/2-thumb.png)](http://image-003.yo2cdn.com/wp-content/uploads/233/23380/2008/05/2.png)

3、确认，在下面的对话框中也点OK确认

[![3](http://image-002.yo2cdn.com/wp-content/uploads/233/23380/2008/05/3-thumb.png)](http://image-002.yo2cdn.com/wp-content/uploads/233/23380/2008/05/3.png)

Profiling Setting

[![5](http://image-003.yo2cdn.com/wp-content/uploads/233/23380/2008/05/5-thumb.png)](http://image-003.yo2cdn.com/wp-content/uploads/233/23380/2008/05/5.png)

系统中已存在的对象

4、设置要记录的数据

[![6](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/6-thumb.png)](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/6.png)

记录对象分配情况

[![7](http://image-003.yo2cdn.com/wp-content/uploads/233/23380/2008/05/7-thumb.png)](http://image-003.yo2cdn.com/wp-content/uploads/233/23380/2008/05/7.png)

记录对类的跟踪数据

[![8](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/8-thumb.png)](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/8.png)

[![9](http://image-002.yo2cdn.com/wp-content/uploads/233/23380/2008/05/9-thumb.png)](http://image-002.yo2cdn.com/wp-content/uploads/233/23380/2008/05/9.png)

在类跟踪选项中添加要跟踪的类

5、开始测试

[![10](http://image-002.yo2cdn.com/wp-content/uploads/233/23380/2008/05/10-thumb.png)](http://image-002.yo2cdn.com/wp-content/uploads/233/23380/2008/05/10.png)

在Concole窗口中按n（代码里边要求的），进入下一步对象的分配和释放阶段

6、测试程序段结束，观察结果

[![11](http://image-003.yo2cdn.com/wp-content/uploads/233/23380/2008/05/11-thumb.png)](http://image-003.yo2cdn.com/wp-content/uploads/233/23380/2008/05/11.png)

运行结束时，有42470个Integer对象尚未回收

[![12](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/12-thumb.png)](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/12.png)

所关注的类的实例数量变化情况，下降的地方是gc起作用了

[![13](http://image-003.yo2cdn.com/wp-content/uploads/233/23380/2008/05/13-thumb.png)](http://image-003.yo2cdn.com/wp-content/uploads/233/23380/2008/05/13.png)

GC在运行过程中，每一时刻回收的对象数量。（GC不是一直运行的，要在JVM的内存不够时才运行）

[![14](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/14-thumb.png)](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/14.png)

所记录的对象的数量变化情况

[![15](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/15-thumb.png)](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/15.png)

堆内存分配变化情况

7、按F4执行gc，观察结果

在第6步看到，运行结束时，有42470个Integer对象尚未回收。现在按F4执行gc

[![16](http://image-002.yo2cdn.com/wp-content/uploads/233/23380/2008/05/16-thumb.png)](http://image-002.yo2cdn.com/wp-content/uploads/233/23380/2008/05/16.png)

Integer对象全部被回收

[![17](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/17-thumb.png)](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/17.png)

[![18](http://image-002.yo2cdn.com/wp-content/uploads/233/23380/2008/05/18-thumb.png)](http://image-002.yo2cdn.com/wp-content/uploads/233/23380/2008/05/18.png)

[![19](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/19-thumb.png)](http://image-001.yo2cdn.com/wp-content/uploads/233/23380/2008/05/19.png)

[![20](http://image-002.yo2cdn.com/wp-content/uploads/233/23380/2008/05/20-thumb.png)](http://image-002.yo2cdn.com/wp-content/uploads/233/23380/2008/05/20.png)

图中的小的尖峰就是对之前未回收的Integer对象进行回收时产生的数据。