---
title: "java内存溢出分析工具：jmap使用实战"
author: "hillside"
date: "2010-07-05T20:25:06Z"
tags: []
categories: ["编程语言"]
slug: "java-jmap"
draft: false
lastmod: "2010-07-05T20:25:06Z"
id: 706129
url: "https://hillside.iteye.com/blog/706129"
---

在一次解决系统tomcat老是内存撑到头，然后崩溃的问题时，使用到了jmap。
1 使用命令
在环境是linux+jdk1.5以上，这个工具是自带的，路径在JDK_HOME/bin/下
**jmap -histo pid>a.log**

2 输出结果摘要
Size    Count   Class description
-------------------------------------------------------
353371288       9652324 char
<span style="color:red">230711112       9612963 java.lang.String</span>
139347160       114865  byte
76128096        3172004 java.util.Hashtable$Entry
<span style="color:red">75782280        3157595 com.test.util.IPSeeker$IPLocation</span>
25724272        9115    java.util.Hashtable$Entry
9319968 166428  org.apache.tomcat.util.buf.MessageBytes
8533856 32889   int

发现有大量的String和自定义对象com.test.util.IPSeeker$IPLocation存在，检查程序发现此处果然存在内存溢出。修改程序上线后再次用jmap抓取内存数据：

146881712   207163  byte
98976352    354285  char
42595272    53558   int
11515632    479818  java.util.HashMap$Entry
9521896 59808   java.util.HashMap$Entry
<span style="color:red">8887392 370308  com.test.bean.UnionIPEntry</span>
8704808 155443  org.apache.tomcat.util.buf.MessageBytes
<span style="color:red">8066880 336120  java.lang.String</span>

内存溢出问题消除。
注意：<span style="color:red">这个jmap使用的时候jvm是处在假死状态的，只能在服务瘫痪的时候为了解决问题来使用，否则会造成服务中断。</span>