简体   繁体   English

Java内存泄漏

[英]Java memory leak

Has anybody used Eclipse memory manager to detect memory leak in java codes? 有没有人使用Eclipse内存管理器来检测java代码中的内存泄漏? Can anybody recommend a good place to look for information regarding using memory manager? 任何人都可以推荐一个寻找有关使用内存管理器的信息的好地方吗? I read something online, it suggests that i need to let the program run until it crashes (out of memory error occurs), which will generates a crash report. 我在线阅读了一些内容,它表明我需要让程序运行直到它崩溃(发生内存不足错误),这将生成崩溃报告。 Then use the memory manager to open this report to examine where the memory leak might occur. 然后使用内存管理器打开此报告以检查可能发生内存泄漏的位置。 Is this how everybody uses memory manager? 这是每个人都使用内存管理器的方式吗?

Perhaps the simplest thing is to run your program under HProf (comes as standard with the JVM) for some time, and force an exit. 也许最简单的方法是在HProf下运行程序(作为JVM的标准配置)一段时间,然后强制退出。 The output of HProf should hopefully give you some immediate pointers re. HProf的输出应该有希望给你一些即时指针。 your memory leak. 你的记忆泄漏。

Unlike the Eclipse memory debugger (which I'm only knowledgeable about from what you write) you can gather statistics from any point in the execution. 与Eclipse内存调试器(我只知道您编写的内容)不同,您可以从执行中的任何位置收集统计信息。

it suggests that i need to let the program run until it crashes (out of memory error occurs), which will generates a crash report. 它表明我需要让程序运行直到它崩溃(发生内存错误),这将生成崩溃报告。

I don't think this is true - you won't get a dump file when an OutOfMemoryError occurs (I would bet the author is confusing this problem with some sort of JVM bug that would cause a core dump to be saved). 我不认为这是真的 - 当OutOfMemoryError发生时你不会得到转储文件(我敢打赌,作者将这个问题与某种导致核心转储被保存的JVM错误混淆)。

The best procedure is to take a heap dump using jmap ; 最好的方法是使用jmap进行堆转储; this will output the contents of the heap to a binary file (usually known as hprof file). 这会将堆的内容输出到二进制文件(通常称为hprof文件)。 This file can then be analyzed by any number of analyzers: 然后,可以通过任意数量的分析器分析此文件:

  • jhat - Sun tool that analyzes hprof file, starts up an embedded web server so you can analyze the heap thru a web browser / view reports. jhat - 分析hprof文件的Sun工具,启动嵌入式Web服务器,以便您可以通过Web浏览器/视图报告分析堆。 I've found that this is very slow for large heaps. 我发现大堆的速度非常慢。
  • VisualVM - Awesome debugging/troubleshooting tool bundled with the JDK. VisualVM - 与JDK捆绑在一起的令人敬畏的调试/故障排除工具。 Among other things, this can also be used to generate a heap dump of any running process, as well as a thread dump. 除此之外,它还可用于生成任何正在运行的进程的堆转储,以及线程转储。 I've found that it's very slow to load large hprof files as well though. 我发现加载大型hprof文件的速度非常慢。
  • Eclipse Memory Analyzer - Eclipse plugin that can generate hprof files. Eclipse Memory Analyzer - 可以生成hprof文件的Eclipse插件。

I would highly recommend using the Eclipse plugin, as it is very quick to load large (> 500MB) heap dumps (in under a minute), produces useful reports, supports a query language with complex logic, etc. 我强烈建议使用Eclipse插件,因为它可以非常快速地加载大型(> 500MB)堆转储(在一分钟内),生成有用的报告,支持具有复杂逻辑的查询语言等。

Though -XX:+HeapDumpOnOutOfMemoryError can be useful, my current workflow for using the Eclipse Memory Manager is: 虽然-XX:+ HeapDumpOnOutOfMemoryError很有用,但我目前使用Eclipse Memory Manager的工作流程是:

  1. Run the program normally 正常运行程序
  2. Wait for memory to get out of hand. 等待记忆失控。
  3. Run jmap : jmap -dump:format=b,file=dump.hprof <PID> 运行jmapjmap -dump:format=b,file=dump.hprof <PID>
  4. Open the hprof file in EMM. 在EMM中打开hprof文件。

I usually start working with the histogram and dominator tree views to see if anything seems out of whack, then drill down from there. 我通常开始使用直方图和支配树视图来查看是否有任何看起来不合适,然后从那里向下钻取。

VisualVM can be useful but seems much less efficient than EMM when working with a heap dump (EMM caches a lot of information on loading the heap dump). VisualVM可能很有用,但在使用堆转储时(EMM缓存有关加载堆转储的大量信息),效率似乎远低于EMM。 Netbeans Profiler is nice for getting the locations of allocations and for time profiling. Netbeans Profiler非常适合获取分配位置和时间分析。

This page explains working with jvm heap dumps. 本页介绍如何使用jvm堆转储。 Jhat is a simpler, if less graphic way of working with heaps, but you can also load the same dump files into the eclipse memory manager. Jhat是一种更简单的,如果使用堆的图形方式更少,但您也可以将相同的转储文件加载到eclipse内存管理器中。 You can also get some information from jvisualvm, if you're using a current(1.6) jvm. 如果您使用的是当前(1.6)jvm,您还可以从jvisualvm获取一些信息。

I generally prefer profiling applications using the NetBeans Profiler. 我通常更喜欢使用NetBeans Profiler分析应用程序。 You can fairly easily see which objects are leaking and where they are created in most cases. 在大多数情况下,您可以非常轻松地查看哪些对象正在泄漏以及它们在何处创建。 There are likely several other tools that will do this as well, but I know the NetBeans profiler works well, and is easy to use. 可能还有其他几种工具可以做到这一点,但我知道NetBeans Profiler运行良好,并且易于使用。

You can try using Jprobe . 您可以尝试使用Jprobe You can monitor your application and can look into the objects as they get created. 您可以监视应用程序,并可以在创建对象时查看它们。 Also this will help analysing what objects dont get garbage collect and will be pointers to move on. 此外,这将有助于分析哪些对象不会被垃圾收集,并将成为继续前进的指针。

Although it is not free, but I remember it comes with a trial licence, so check for that. 虽然它不是免费的,但我记得它带有试用许可证,所以检查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM