java内存泄漏排除笔记
最近应用不定期的会挂得,表现是java堆内存用光,CPU 100%,应用没有响应。用jmap -histo pid(进程号),dump出当前内存java对象信息,
num #instances #bytes class name
----------------------------------------------
1: 23883422 1383659120 [Ljava.lang.Object;
2: 38566123 1234115936 java.lang.String
3: 38576360 1037800072 [C
4: 31742957 1015774624 com.qidea.gameserver.subsys.battlesys.BattleTapeUnit
5: 10038906 803112480 com.qidea.gameserver.subsys.battlesys.BattleTapeUnitHurt
6: 23865403 572769672 java.util.ArrayList
7: 7935551 444390856 com.qidea.gameserver.subsys.battlesys.BattleTapeUnitFight
8: 11808162 283397352 [F
9: 6605214 211366848 java.util.HashMap$Entry
10: 3122289 149869872 com.qidea.game.map.MapLandTable
11: 66126 94220344 [B
12: 82414 43991784 [Ljava.util.HashMap$Entry;
13: 118473 17773488 <constMethodKlass>
14: 118473 16124280 <methodKlass>
15: 13767 14909176 <constantPoolKlass>
16: 12466 10045840 <constantPoolCacheKlass>
17: 13767 9999456 <instanceKlassKlass>
18: 232564 9155008 [I
19: 159971 9122656 <symbolKlass>
20: 126653 7092568 org.apache.tomcat.util.buf.MessageBytes
21: 147424 7076352 org.apache.tomcat.util.buf.ByteChunk
22: 211044 6753408 java.sql.Timestamp
23: 129619 5184760 org.apache.tomcat.util.buf.CharChunk
24: 7213 4683984 <methodDataKlass>
25: 82978 4646768 org.objectweb.asm.Item
26: 52466 4617008 java.lang.reflect.Method
27: 188251 4518024 org.objectweb.asm.Edge
28: 58425 3739200 org.objectweb.asm.Label
29: 63797 3062256 java.util.HashMap
30: 125571 3013704 java.lang.Long
31: 64031 2561240 java.util.LinkedHashMap$Entry
32: 71741 1853696 [Ljava.lang.String;
33: 44056 1762240 java.lang.ref.SoftReference
34: 108406 1734496 java.lang.Integer
35: 35404 1699392 com.qidea.gameserver.subsys.herosys.RoleBattlearray
36: 14821 1541384 com.qidea.gameserver.subsys.citysys.City
37: 14421 1499784 java.lang.Class
38: 41691 1334112 java.util.Hashtable$Entry
39: 23833 1143984 org.aspectj.weaver.reflect.ShadowMatchImpl
40: 69888 1118208 java.lang.Float
41: 33705 1078560 java.util.concurrent.ConcurrentHashMap$HashEntry
42: 33548 1073536 org.apache.tomcat.util.http.MimeHeaderField
43: 33529 1072928 java.lang.ref.WeakReference
44: 5476 1051392 org.objectweb.asm.MethodWriter
----------------------------------------------
1: 23883422 1383659120 [Ljava.lang.Object;
2: 38566123 1234115936 java.lang.String
3: 38576360 1037800072 [C
4: 31742957 1015774624 com.qidea.gameserver.subsys.battlesys.BattleTapeUnit
5: 10038906 803112480 com.qidea.gameserver.subsys.battlesys.BattleTapeUnitHurt
6: 23865403 572769672 java.util.ArrayList
7: 7935551 444390856 com.qidea.gameserver.subsys.battlesys.BattleTapeUnitFight
8: 11808162 283397352 [F
9: 6605214 211366848 java.util.HashMap$Entry
10: 3122289 149869872 com.qidea.game.map.MapLandTable
11: 66126 94220344 [B
12: 82414 43991784 [Ljava.util.HashMap$Entry;
13: 118473 17773488 <constMethodKlass>
14: 118473 16124280 <methodKlass>
15: 13767 14909176 <constantPoolKlass>
16: 12466 10045840 <constantPoolCacheKlass>
17: 13767 9999456 <instanceKlassKlass>
18: 232564 9155008 [I
19: 159971 9122656 <symbolKlass>
20: 126653 7092568 org.apache.tomcat.util.buf.MessageBytes
21: 147424 7076352 org.apache.tomcat.util.buf.ByteChunk
22: 211044 6753408 java.sql.Timestamp
23: 129619 5184760 org.apache.tomcat.util.buf.CharChunk
24: 7213 4683984 <methodDataKlass>
25: 82978 4646768 org.objectweb.asm.Item
26: 52466 4617008 java.lang.reflect.Method
27: 188251 4518024 org.objectweb.asm.Edge
28: 58425 3739200 org.objectweb.asm.Label
29: 63797 3062256 java.util.HashMap
30: 125571 3013704 java.lang.Long
31: 64031 2561240 java.util.LinkedHashMap$Entry
32: 71741 1853696 [Ljava.lang.String;
33: 44056 1762240 java.lang.ref.SoftReference
34: 108406 1734496 java.lang.Integer
35: 35404 1699392 com.qidea.gameserver.subsys.herosys.RoleBattlearray
36: 14821 1541384 com.qidea.gameserver.subsys.citysys.City
37: 14421 1499784 java.lang.Class
38: 41691 1334112 java.util.Hashtable$Entry
39: 23833 1143984 org.aspectj.weaver.reflect.ShadowMatchImpl
40: 69888 1118208 java.lang.Float
41: 33705 1078560 java.util.concurrent.ConcurrentHashMap$HashEntry
42: 33548 1073536 org.apache.tomcat.util.http.MimeHeaderField
43: 33529 1072928 java.lang.ref.WeakReference
44: 5476 1051392 org.objectweb.asm.MethodWriter
45: ...............................................................
发现BattleTapeUnit,BattleTapeUnitHurt,BattleTapeUnitFight这几个对象比较异常,根据这些类信息,到代码中找出跟这些类相关的代码,排除出了一些问题。修改相关代码后,重新发布应用,跑了一周,内存都很平稳,问题解决。
之前在网上看到过用jmap把堆内存镜象都dump下来,用Eclipse MAT来分析的这种方法来解决内存泄漏问题,我觉得没有必要,首先我应用挂掉后,堆都是满的,这时dump出来的内存镜象少说几百M,大了几个G。我们应用就是8个G,这么大的镜象从正式环境下载下来就比较麻烦,其次用MAT分析,信息太多,没办法一下子抓到重要信息。相比而言使用jmap -histo,来的更简单更快捷一些。