Java: Finding out what is using all the memory
I have a java application that runs out of memory, but I have no idea which code is allocating the memory. Is there an app开发者_JAVA技巧lication with which I can check this? I use Eclipse.
Since you use eclipse I would recommend the Eclipse Memory Analyzer plugin. This tool works great, and will even provide a report with some likely leak suspects. I have looked at over 1G heap dumps with this with no problems. I just use jconsole included with the JDK to get the dump.
Also here is a great blog on using the tool, by one of the authors of the tool.
This is also free.
VisualVM is a visual tool integrating several commandline JDK tools and lightweight profiling capabilities. Designed for both production and development time use, it further enhances the capability of monitoring and performance analysis for the Java SE platform.
And it is included in JDK.
I've had great success with JProfiler
http://www.ej-technologies.com/products/jprofiler/overview.html
You want a Profiler.
Here is a comprehensive list.
You need to use a memory profiler. You can either use Sun's JVM Tool Interface or a third-party profiler like JProfiler. Eclipse also has profiling tools, called the Test & Performance Tools Platform (TPTP).
I'm sure everyone has their favorite, but I recommend Netbeans.
Yourkit is a great tool that I've used on MANY occasions to find and document performance issues. Eclipse with the TPTP framework can be used to do this as well. It also works well against remote application servers (you need to enable profiling agents and turn on remote debugging in the JVM), but it lets you run profiling against pretty much any J2EE environment.
For fast profiling or if you don't use Eclipse or older versions of the jvm, hprof is a decent alternative. (I just add that as a reference.).
I had to find the source of a memory leak that produced a 2.6G heap dump the other day. Jhat required an unbelievable 20G heap size to crunch through it without crashing. Eclipse memory analyzer did it in under 10G. That said, I found both tools useful, but would recommend memory analyzer first for more efficient use of memory and being more easy to use.
I was pretty amazed by the amount of memory used but I can assure you if I used less I head OutOfMemory errors.
I've spotted memory leaks using HP Diagnostics Profiler free evaluation (unlimited for 5 threads). It allows to trace memory allocation and do heap analysis in a visual, easy way.
精彩评论