I'm looking at a memory issue in some code in a few classes that run in a web app. I'm using Glassfish3.1 and have a fairly straight forward REST web app with some Java EE elements (it uses JMS to do some offline processing, etc). The problem is that when I test a part of the app under stress and monitor the server (a Linux server) using top I see the CPU usage at nice acceptable levels for about 20 minutes them BAM! 98-100% levels for ever after. I need to profile the app to figure out what is owning the CPU but I've never used a profiler before and am baffled by all of the online documentation I've found so far. I was hoping to use HPROF since it's built-in but I can not figure out the correct steps to use it.
Is there a plain English document that explains (with examples) how to use HPROF on a web app running in a server (in my case Glassfish 3.1) and read the CPU profile data?
I will accept another solution so long as I can get it easily for free (i.e. I can't use jProfiler because the license costs money), though my preference would be to use hprof.
Badal Chowdhary wrote:VisualVM is a free Java profiling tool. I did use this to play around and got a basic Java program attached to this profiler. What is the state of your heap memory?
I have a web application with one Restful web service that blows out memory. It happens in a single thread that's grabbing messages off a JMS queue, turning them into Java objects and then storing them in a MySQL db. It runs great for about a half hour (when I'm stress testing) with mem usage not climbing up more that when that thread in the app isn't running and then without warning, BLAM! mem usage ramps up and inside a 3-5 second window I start seeing heap errors.
I've seen other profiling tools that look easy to hook up to a simple Java app but I need to attach this to a web app that's running inside glassfish. How easy is it to do that with VisualVM? I've had no luck finding any instructions on a free profiler that can do that.
If you are getting heap memory error, that means objects are being created but are not de-referenced when not needed. As a result, Java garbage collector cannot free up memory on heap. There are quite a few tools to analyze heap. All you have to do is set up a JVM parameter to do a heap dump on first occurance of OutOfMemoryError. I had written a post that does this using Eclipse Memory Analyzer. Check out if this is helpful: