java 我可以让 Tomcat 作为转储堆的服务运行吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2172220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I get Tomcat running as a service to dump heap?
提问by Laird Nelson
I am attempting to have Tomcat, which is currently running as a service on a Windows 2003 box, dump heap on an OutOfMemoryError.
我正在尝试让 Tomcat(目前在 Windows 2003 机器上作为服务运行)将堆转储到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.)
(Tomcat 正在运行 Hudson,它在我构建的末尾报告了堆空间问题。手动运行构建不会产生此类错误。Hudson 人员需要一个堆转储才能开始。)
As instructed elsewhere, I've told the Apache Service Monitor to configure the JVM it uses to run Tomcat to dump heap when an OutOfMemoryErroris 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.hproffile (where obviously 123is replaced by the PID of the JVM). No .hproffiles exist anywhere.
按照其他地方的指示,我已经告诉 Apache 服务监视器配置它用来运行 Tomcat 的 JVM,以便在OutOfMemoryError遇到 JVM 选项时转储堆: -XX:+HeapDumpOnOutOfMemoryError 然后我再次运行构建。果然,它报告有一个堆错误。我扫描整个磁盘以寻找默认java_pid123.hprof文件(显然123已被 JVM 的 PID 替换)。任何.hprof地方都不存在文件。
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.
我遇到了问题 22:我需要 Hudson 人员的堆转储来修复他们的内存泄漏,但是如果我在 Tomcat 下运行 Hudson,我将无法获得堆转储。
Is there some special way, when Tomcat is running as a Windows service, to get a heap dump from it on an OutOfMemoryError?
当 Tomcat 作为 Windows 服务运行时,是否有一些特殊的方法可以在 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.dlldirectly. When I do this, the service won't start.
我尝试过的另一件事是在“启动”和“关闭”选项卡上告诉它使用“Java”选项而不是“jvm”选项。我相信这应该告诉服务管理器尝试使用 Java 可执行命令启动 Tomcat,而不是jvm.dll直接启动 Tomcat 。当我这样做时,服务将无法启动。
Surely someone else has had a similar problem?
肯定有人遇到过类似的问题吗?
采纳答案by Laird Nelson
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 .exeinstaller, 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 .hproffiles in the current directory, and no amount of Windows security tweaking appears to make this problem go away.
首先,如果您在 Windows 上安装 Tomcat,请不要使用.exe安装程序,即使它是由 Apache 推广的。无论您做什么,它都不会让您以系统帐户以外的任何身份运行 Tomcat。系统帐户似乎没有.hprof在当前目录中写入文件的权限,并且似乎没有多少 Windows 安全调整可以使这个问题消失。
OK, so you've installed Tomcat from the .zipdistribution. Install it as a service using the service.batscript. 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.
好的,您已经从.zip发行版安装了 Tomcat 。使用service.bat脚本将其安装为服务。确保它设置为作为您专门为此目的创建的特定用户运行。还要确保您希望 Tomcat 在发生堆转储时写入的文件夹可由该用户写入。
Edit the service.batfile to include the -XX:+HeapDumpOnOutOfMemoryErrorand the -XX:HeapDumpPath=C:\whateveroptions in the correct place (where you can put JVM options). That should do the trick.
编辑service.bat文件以在正确的位置包含-XX:+HeapDumpOnOutOfMemoryError和-XX:HeapDumpPath=C:\whatever选项(您可以在其中放置 JVM 选项)。这应该够了吧。
回答by Gennady Shumakher
Have you tried -XX:HeapDumpPath option?
您是否尝试过 -XX:HeapDumpPath 选项?
http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp
http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp
回答by Peter Schuetze
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.
我找到了以下链接,其中描述了如何配置tomcat服务(包括设置java参数)。不确定它是否适用于您正在运行的版本。
http://tomcat.apache.org/tomcat-5.5-doc/windows-service-howto.html
http://tomcat.apache.org/tomcat-5.5-doc/windows-service-howto.html
回答by Adeel Ahmad
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.
如果您使用 .exe 安装了 tomcat,您可以将 tomcat 服务配置为使用本地系统帐户以外的帐户,并且您可以在创建转储文件的目录“c:\whatever”上分配该用户权限。这里要记住的一件事是,tomcat 服务不能使用具有管理权限的帐户运行。所以在windows中创建一个简单的用户(用户组的成员)并将tomcat服务设置为用户这个帐户。并授予该用户对“c:\whatever”目录的权限。这解决了用户目录权限问题,但您必须为内存转储错误配置 tomcat。
回答by Anil Agrawal
When java process running as window service you can generate the heapdump using below steps,
当 java 进程作为窗口服务运行时,您可以使用以下步骤生成堆转储,
- Run the command console as Administrator
- version of JDK (for jmap command) and JRE (Java app run environment) should be same.
- Get the PID no of running window process for that java application from task manager
- Execute below command
- 以管理员身份运行命令控制台
- JDK(用于 jmap 命令)和 JRE(Java 应用程序运行环境)的版本应该相同。
- 从任务管理器中获取该 Java 应用程序正在运行的窗口进程的 PID 编号
- 执行以下命令
jmap -dump:file=d:\heapdump\myHeapDump.hprof -F #PID_No#
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
如果 JDK/JRE 7 有任何异常,请尝试使用 JDK/JRE 8 实际上我在使用 JDK 7 的 jmap 中遇到了一些问题,但是当我移动到 JDK 8 时,我能够使用相同的命令成功生成堆转储
回答by stacker
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
将以下内容添加到 catalina.sh(或适用于 Windows 的 catalina.bat)中的 JAVA_OPTS 中:-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/j2ee/heapdumps
回答by skaffman
The .hproffiles are dumped in the current directory. Exactly what that means for a windows service is anyone's guess, assuming it means anything.
该.hprof文件转储在当前目录。任何人都猜测这对 Windows 服务意味着什么,假设它意味着什么。
I suggest posting a new question (on http://superuser.com) asking what "current directory" means for a windows service.
我建议发布一个新问题(在http://superuser.com 上),询问“当前目录”对于 Windows 服务的含义。

