3

学习修复 java 内存泄漏的最佳地点是什么?我一直试图通过 NET 找到好的资源,但令我失望的是,我发现正在讨论的玩具示例。我还能够解决小型玩具转储问题,但现实世界的应用程序转储更具挑战性并且几乎没有提供任何线索。

我尝试过 Jhat、JMap、VisualVM 和 MAT 等工具。

学习修复 Java 内存泄漏的最佳地点是什么?也欢迎推荐一本书。

提前致谢。

4

2 回答 2

2

There are many kind of memory leaks but in practice the following strategy gives good results (Disclaimer: I'm showing screen shots from JProfiler, because my company develops it):

1. Check the biggest objects

"Biggest objects" retain a lot of memory that nobody else is holding on to. In JProfiler, they can be found in the "Biggest objects" view of the heap walker. This often happens with caches.

enter image description here

2. Check classes with a lot of instances

Sometimes, the references are more intricate and the biggest objects do not give a clue. Classes with a lot of instances can be checked for their incoming references. First, it is helpful to look at cumulated references, to separate valid reference chains from suspicious reference chains.

enter image description here

Also, checking where instances have been allocated (as opposed to how they are referenced) can often give an insight on what is going on.

enter image description here

Often, this is a multi-step process, selecting reference chains, allocation spots, or making selections based on the contents of objects.

3. Search for paths to GC roots

If that still does not provide a clue, you can select single objects and show their reference chains that lead to garbage collector roots. It helps a lot of you were able to narrow down an object set of suspicious objects before trying to to that, otherwise you may have a lot of candidates.

enter image description here

There is also a screen cast on this topic.

于 2012-07-31T09:13:17.753 回答
2

你试过搜索互联网吗?有很多例子。一个好的搜索引擎会按相关性对它们进行排序。

于 2012-07-30T18:27:35.413 回答