Can I get Tomcat running as a service to dump heap

2019-03-25 15:24发布

问题:

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?

回答1:

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.



回答2:

Have you tried -XX:HeapDumpPath option?

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



回答3:

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



回答4:

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.



回答5:

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



回答6:

if you have installed tomcat with .exe you can configure tomcat service to use account other than local system account and you can assign that user rights on directory "c:\whatever" where you are creating your dump file. one thing here to remember tomcat service don't run with account having administrative privileges. so create a simple user in windows(member of user group) and set tomcat services to user this account. and give that user rights on "c:\whatever" directory. This resolves the user directory rights issue but you have to configure tomcat for Memory dumps errors.



回答7:

When java process running as window service you can generate the heapdump using below steps,

  1. Run the command console as Administrator
  2. version of JDK (for jmap command) and JRE (Java app run environment) should be same.
  3. Get the PID no of running window process for that java application from task manager
  4. Execute below command

jmap -dump:file=d:\heapdump\myHeapDump.hprof -F #PID_No#

If got any exception with JDK/JRE 7 try the same with JDK/JRE 8 Actually I faced some issue in jmap with JDK 7, but when i moved to JDK 8, I were able to successfully generate the heap dump using same command