views:

614

answers:

5

I am attempting to have Tomcat, which is currently running as a service on a Windows 2003 box, dump heap on an OutOfMemoryError.

(Tomcat is running Hudson, which is reporting a heap space problem at the tail end of my build. Running the build manually produces no such error. The Hudson guys need a heap dump to get started.)

As instructed elsewhere, I've told the Apache Service Monitor to configure the JVM it uses to run Tomcat to dump heap when an OutOfMemoryError is encountered by adding the following to the JVM options: -XX:+HeapDumpOnOutOfMemoryError Then I run the build again. Sure enough, it reports there was a heap error. I scan the entire disk looking for the default java_pid123.hprof file (where obviously 123 is replaced by the PID of the JVM). No .hprof files exist anywhere.

I am caught in a catch 22: I need the heap dump for the Hudson guys to fix their memory leak, but I can't get the heap dump if I run Hudson under Tomcat.

Is there some special way, when Tomcat is running as a Windows service, to get a heap dump from it on an OutOfMemoryError?

The other thing I've tried is to tell it, on the Startup and Shutdown tabs, to use the "Java" option instead of the "jvm" option. I believe this should tell the Service Manager to attempt to start Tomcat with a Java executable command instead of launching the jvm.dll directly. When I do this, the service won't start.

Surely someone else has had a similar problem?

A: 

The .hprof files are dumped in the current directory. Exactly what that means for a windows service is anyone's guess, assuming it means anything.

I suggest posting a new question (on http://superuser.com) asking what "current directory" means for a windows service.

skaffman
Current directory, perhaps, but no .hprof file is present on the machine at all.
Laird Nelson
So it's possible that windows services have no "current directory", and windows is dumping the file into the void. My suggestion about finding out more about windows services stands.
skaffman
The service monitor claims the current directory is C:\Program Files\Apache Tomcat\Tomcat 6.0. I've written a servlet that outputs a file to the current directory, and the file shows up in that directory.
Laird Nelson
+2  A: 

Have you tried -XX:HeapDumpPath option?

http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp

Gennady Shumakher
Yes, it has no effect.
Laird Nelson
A: 

From 20 Tips for Using Tomcat in Production

Add the following to your JAVA_OPTS in catalina.sh (or catalina.bat for Windows): -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/j2ee/heapdumps

stacker
The problem of course is that neither script is invoked by the service monitor.
Laird Nelson
+1  A: 

I found the following link, which describes how to configure the tomcat service (includes setting the java parameters). Not sure if it applies to the version you are running.

http://tomcat.apache.org/tomcat-5.5-doc/windows-service-howto.html

Peter Schuetze
Thank you; that is indeed helpful, but I should have mentioned I'm running Tomcat 6.
Laird Nelson
+2  A: 

After finally putting this one to bed, I wanted to answer this for others who might have the same problem.

First, if you install Tomcat on Windows, do not use the .exe installer, even though it is promoted by Apache. It will not let you run Tomcat as anything other than the system account, no matter what you do. It appears that the system account does not have privileges to write .hprof files in the current directory, and no amount of Windows security tweaking appears to make this problem go away.

OK, so you've installed Tomcat from the .zip distribution. Install it as a service using the service.bat script. Make sure it is set to run as a specific user that you created specifically for this purpose. Make sure as well that the folder you want Tomcat to write to in the event of a heap dump is writable by that user.

Edit the service.bat file to include the -XX:+HeapDumpOnOutOfMemoryError and the -XX:HeapDumpPath=C:\whatever options in the correct place (where you can put JVM options). That should do the trick.

Laird Nelson