如何增加 tomcat 应用程序的 Java 堆空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2718786/
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
How to increase Java heap space for a tomcat app
提问by Ankur
There are lots of questions that ask this or a similar question.
有很多问题会问这个或类似的问题。
They all give the command that has to be executed, what I don't understand is where do I write this command. I want to permanently increase the heap space for my tomcat apps.
他们都给出了必须执行的命令,我不明白的是我在哪里写这个命令。我想永久增加我的 tomcat 应用程序的堆空间。
I read this page http://javahowto.blogspot.com/2006/06/6-common-errors-in-setting-java-heap.htmland it says under the Tomcat section
我读了这个页面 http://javahowto.blogspot.com/2006/06/6-common-errors-in-setting-java-heap.html,它在Tomcat部分下说
Stop Tomcat server, set environment variable CATALINA_OPTS, and then restart Tomcat. Look at the file tomcat-install/bin/catalina.sh or catalina.bat for how this variable is used. For example,
set CATALINA_OPTS=-Xms512m -Xmx512m (Windows, no "" around the value)
export CATALINA_OPTS="-Xms512m -Xmx512m" (ksh/bash, "" around the value)
setenv CATALINA_OPTS "-Xms512m -Xmx512m" (tcsh/csh, "" around the value)
停止Tomcat服务器,设置环境变量CATALINA_OPTS,然后重启Tomcat。查看文件 tomcat-install/bin/catalina.sh 或 catalina.bat 了解如何使用此变量。例如,
设置 CATALINA_OPTS=-Xms512m -Xmx512m(Windows,值周围没有 "")
export CATALINA_OPTS="-Xms512m -Xmx512m" (ksh/bash, "" 周围的值)
setenv CATALINA_OPTS "-Xms512m -Xmx512m",tcsh "" 周围的值)
So I replaced the line
所以我换了线
set CATALINA_OPTS=
设置 CATALINA_OPTS=
with
和
set CATALINA_OPTS=-Xms512m -Xmx512m
设置 CATALINA_OPTS=-Xms512m -Xmx512m
But I still get the error.
但我仍然收到错误。
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.OutOfMemoryError: Java heap space java.lang.reflect.Array.multiNewArray(Native Method) java.lang.reflect.Array.newInstance(Array.java:90) nom.tam.util.ArrayFuncs.newInstance(ArrayFuncs.java:1028) nom.tam.fits.ImageData.read(ImageData.java:259) nom.tam.fits.Fits.readHDU(Fits.java:573) controller.UploadServlet.retreiveFITSFileFields(UploadServlet.java:206) controller.ScanServerFiles.doPost(ScanServerFiles.java:39) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.ServletException:Servlet 执行引发异常
根本原因
java.lang.OutOfMemoryError: Java 堆空间 java.lang.reflect.Array.multiNewArray(Native Method) java.lang.reflect.Array.newInstance(Array.java:90) nom.tam.util.ArrayFuncs.newInstance(ArrayFuncs. java:1028) nom.tam.fits.ImageData.read(ImageData.java:259) nom.tam.fits.Fits.readHDU(Fits.java:573) controller.UploadServlet.retreiveFITSFileFields(UploadServlet.java:206) 控制器。 ScanServerFiles.doPost(ScanServerFiles.java:39) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
采纳答案by mdma
Your change may well be working. Does your application need a lot of memory - the stack trace shows some Image related features.
你的改变很可能奏效。您的应用程序是否需要大量内存 - 堆栈跟踪显示了一些与图像相关的功能。
I'm guessing that the error either happens right away, with a large file, or happens later after several requests.
我猜测错误要么立即发生,文件很大,要么在多次请求后发生。
If the error happens right away, then you can increase memory still further, or investigate find out why so much memory is needed for one file.
如果错误立即发生,那么您可以进一步增加内存,或者调查找出为什么一个文件需要这么多内存。
If the error happens after several requests, then you could have a memory leak - where objects are not being reclaimed by the garbage collector. Using a tool like JProfilercan help you monitor how much memory is being used by your VM and can help you see what is using that memory and why objects are not being reclaimed by the garbage collector.
如果错误发生在几次请求之后,那么您可能会发生内存泄漏 - 垃圾收集器没有回收对象。使用JProfiler 之类的工具 可以帮助您监控 VM 使用了多少内存,并可以帮助您查看哪些内容正在使用该内存以及垃圾收集器未回收对象的原因。
回答by cherouvim
First of all you cannot change the memory settings only for a tomcat application but rather for all tomcat instance.
首先,您不能仅针对 tomcat 应用程序更改内存设置,而可以针对所有 tomcat 实例更改内存设置。
If you are running tomcat from console (using startup.bat) you'll need to edit catalina.bat and play around with CATALINA_OPTS. For example:
如果您从控制台运行 tomcat(使用 startup.bat),您需要编辑 catalina.bat 并使用 CATALINA_OPTS。例如:
set CATALINA_OPTS=-Xms512m -Xmx512m
Restarting tomcat will apply the new settings.
重新启动 tomcat 将应用新设置。
If you are still getting OutOfMemoryError you need to know how much memory does your application need at that particular moment (nom.tam.util.ArrayFuncs...). You'll either have to optimize the application or simply increase the memory provided to tomcat.
如果您仍然遇到 OutOfMemoryError,您需要知道您的应用程序在那个特定时刻需要多少内存(nom.tam.util.ArrayFuncs...)。您要么必须优化应用程序,要么只是增加提供给 tomcat 的内存。
回答by Juris
There is a mechanism to do it without modifying any files that are in the distribution. You can create a separate file %CATALINA_HOME%\bin\setenv.bat
or $CATALINA_HOME/bin/setenv.sh
and put your environment variables there. Further, the memory settings apply to the JVM, not Tomcat, so I'd set the JAVA_OPTS
variable instead:
有一种机制可以在不修改分发中的任何文件的情况下执行此操作。您可以创建一个单独的文件%CATALINA_HOME%\bin\setenv.bat
或$CATALINA_HOME/bin/setenv.sh
将您的环境变量放在那里。此外,内存设置适用于 JVM,而不适用于 Tomcat,因此我将JAVA_OPTS
改为设置变量:
set JAVA_OPTS=-Xmx512m
set JAVA_OPTS=-Xmx512m
回答by Karthik Reddy
- Open the server tab in eclipse
- right click open
- click on open lauch configuration
- Go to arguments
Here you can add in VM arguments after endorsed
-Xms64m -Xmx256m
- 在 Eclipse 中打开服务器选项卡
- 右键打开
- 点击打开lauch配置
- 转到参数
在这里你可以在背书后添加VM参数
-Xms64m -Xmx256m
回答by horaT
if you are using Windows, it's very simple. Just go to System Environnement variables (right-clic Computer > Properties > Advanced System Parameters > Environnement Variables); create a new system variable with name = CATALINA_OPTS and value = -Xms512m -Xmx1024m. restart Tomcat and enjoy!
如果您使用的是 Windows,则非常简单。只需转到系统环境变量(右键单击计算机 > 属性 > 高级系统参数 > 环境变量);创建一个名称 = CATALINA_OPTS 且值 = -Xms512m -Xmx1024m 的新系统变量。重新启动Tomcat并享受!
回答by Aniket Thakur
You need to add the following lines in your catalina.sh
file.
您需要在catalina.sh
文件中添加以下几行。
export CATALINA_OPTS="-Xms512M -Xmx1024M"
UPDATE: catalina.sh
content clearly says -
更新:catalina.sh
内容清楚地表明-
Do not set the variables in this script. Instead put them into a script setenv.sh in CATALINA_BASE/bin to keep your customizations separate.
不要在此脚本中设置变量。而是将它们放入 CATALINA_BASE/bin 中的脚本 setenv.sh 中,以将您的自定义设置分开。
So you can add above in setenv.sh instead (create a file if it does not exist).
因此,您可以在 setenv.sh 中添加上面的内容(如果文件不存在,则创建一个文件)。
回答by Vivex
you can set this in catalina.sh as CATALINA_OPTS=-Xms512m -Xmx512m
您可以在 catalina.sh 中将其设置为 CATALINA_OPTS=-Xms512m -Xmx512m
Open your tomcat-dir/bin/catalina.sh file and add following line anywhere -
打开您的 tomcat-dir/bin/catalina.sh 文件并在任何地方添加以下行 -
CATALINA_OPTS="$CATALINA_OPTS -Xms1024m -Xmx3024m"
and restart your tomcat
并重新启动你的tomcat
回答by Prashant Gupta
Just set this extra line in catalina.bat
file
只需在catalina.bat
文件中设置这一行
LINE NO AROUND: 143
周围没有线路:143
set "CATALINA_OPTS=-Xms512m -Xmx512m"
And restart Tomcat service
并重启Tomcat服务
回答by Ammad
Easiest way of doing is: (In Linux/Ububuntu e.t.c)
最简单的方法是:(在 Linux/Ububuntu 等中)
Go to tomcat bin directory:
进入 tomcat bin 目录:
cd /opt/tomcat8.5/bin
create new file under bin directory "setenv.sh" and save below mention entries in it.
在 bin 目录“setenv.sh”下创建新文件并将以下提及的条目保存在其中。
export CATALINA_OPTS="$CATALINA_OPTS -Xms512m"
export CATALINA_OPTS="$CATALINA_OPTS -Xmx2048m"
export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxPermSize=256m"
and issue command:
并发出命令:
./catalina.sh run
In your catalina log file you can see entry like this:
在您的 catalina 日志文件中,您可以看到如下条目:
INFO [main] VersionLoggerListener.log Command line argument: -Xms512m
INFO [main] VersionLoggerListener.log Command line argument: -Xmx2048m
INFO [main] VersionLoggerListener.log Command line argument: -XX:MaxPermSize=256m
Which confirms that above changes took place.
这证实了上述变化发生了。
Also, the value of "Xms512m" and "-Xmx2048m" can be modified accordingly in the setenv.sh file.
此外,可以在 setenv.sh 文件中相应地修改“Xms512m”和“-Xmx2048m”的值。
Startup of tomcat could be done in two steps as well. cd /opt/tomcat8.5/bin
tomcat的启动也可以分两步完成。cd /opt/tomcat8.5/bin
Step #1
run ./setenv.sh
Step #2
./startup.sh
If you're using systemd edit:
如果您使用 systemd 编辑:
/usr/lib/systemd/system/tomcat8.service
and set
并设置
Environment=CATALINA_OPTS="-Xms512M -Xmx2048M -XX:MaxPermSize=256m"
回答by Scott Wardlaw
For Windows Service, you need to run tomcat9w.exe (or 6w/7w/8w) depending on your version of tomcat. First, make sure tomcat is stopped. Then double click on tomcat9w.exe. Navigate to the Java tab. If you know you have 64 bit Windows with 64 bit Java and 64 bit Tomcat, then feel free to set the memory higher than 512. You'll need to do some task manager monitoring to determine how high to set it. For most apps developed in 2019... I'd recommend an initial memory pool of 1024, and the maximum memory pool of 2048. Of course if your computer has tons of RAM... feel free to go as high as you want. Also, see this answer: How to increase Maximum Memory Pool Size? Apache Tomcat 9
对于 Windows 服务,您需要根据您的 tomcat 版本运行 tomcat9w.exe(或 6w/7w/8w)。首先,确保 tomcat 已停止。然后双击 tomcat9w.exe。导航到 Java 选项卡。如果您知道您有 64 位 Windows 和 64 位 Java 和 64 位 Tomcat,那么可以随意将内存设置为高于 512。您需要进行一些任务管理器监控以确定将其设置为多高。对于 2019 年开发的大多数应用程序...我建议初始内存池为 1024,最大内存池为 2048。当然,如果您的计算机有大量 RAM...请随意使用。另外,请参阅此答案:如何增加最大内存池大小?Apache Tomcat 9