Can I get Tomcat running as a service to dump heap?

萝らか妹 提交于 2019-12-03 12:03:05

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.

Have you tried -XX:HeapDumpPath option?

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

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

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.

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!