如何查找 Java 内存泄漏 [英] How to find a Java Memory Leak

查看:32
本文介绍了如何查找 Java 内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何发现 Java 中的内存泄漏(例如,使用 JHat)?我尝试在 JHat 中加载堆转储以进行基本查看.但是,我不明白我应该如何找到根引用(ref) 或任何名称.基本上,我可以说有几百兆字节的哈希表条目([java.util.HashMap$Entry 或类似的东西),但是到处都在使用地图......有没有办法搜索大地图,或者可能找到大对象树的一般根?

How do you find a memory leak in Java (using, for example, JHat)? I have tried to load the heap dump up in JHat to take a basic look. However, I do not understand how I am supposed to be able to find the root reference (ref) or whatever it is called. Basically, I can tell that there are several hundred megabytes of hash table entries ([java.util.HashMap$Entry or something like that), but maps are used all over the place... Is there some way to search for large maps, or perhaps find general roots of large object trees?

好的,到目前为止我已经阅读了答案,但让我们说我是一个廉价的混蛋(这意味着我对学习如何使用 JHat 比为 JProfiler 付费更感兴趣).此外,JHat 始终可用,因为它是 JDK 的一部分.当然,除非 JHat 没有办法只能使用蛮力,但我不敢相信情况会如此.

Ok, I've read the answers so far but let's just say I am a cheap bastard (meaning I am more interested in learning how to use JHat than to pay for JProfiler). Also, JHat is always available since it is part of the JDK. Unless of course there is no way with JHat but brute force, but I can't believe that can be the case.

此外,我认为我无法实际修改(添加所有地图大小的日志记录)并将其运行足够长的时间让我注意到泄漏.

Also, I do not think I will be able to actually modify (adding logging of all map sizes) and run it for long enough for me to notice the leak.

推荐答案

我使用以下方法来查找 Java 中的内存泄漏.我已经使用 jProfiler 取得了巨大的成功,但我相信任何具有绘图功能(差异更容易以图形形式分析)的专业工具都可以使用.

I use following approach to finding memory leaks in Java. I've used jProfiler with great success, but I believe that any specialized tool with graphing capabilities (diffs are easier to analyze in graphical form) will work.

  1. 启动应用程序并等待它进入稳定"状态,此时所有初始化都已完成且应用程序处于空闲状态.
  2. 多次运行怀疑产生内存泄漏的操作,以允许进行任何缓存、与数据库相关的初始化.
  3. 运行 GC 并拍摄内存快照.
  4. 再次运行该操作.根据操作的复杂性和所处理数据的大小,操作可能需要运行数次或多次.
  5. 运行 GC 并拍摄内存快照.
  6. 运行 2 个快照的差异并进行分析.

基本上分析应该从最大的正差异开始,比如对象类型,并找出导致这些额外对象留在内存中的原因.

Basically analysis should start from greatest positive diff by, say, object types and find what causes those extra objects to stick in memory.

对于在多个线程中处理请求的 Web 应用程序,分析变得更加复杂,但一般方法仍然适用.

For web applications that process requests in several threads analysis gets more complicated, but nevertheless general approach still applies.

我做了很多专门旨在减少应用程序内存占用的项目,这种带有一些特定于应用程序的调整和技巧的通用方法总是很有效.

I did quite a number of projects specifically aimed at reducing memory footprint of the applications and this general approach with some application specific tweaks and trick always worked well.

这篇关于如何查找 Java 内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆