如果您遇到Tomcat的性能问题,一个常见的原因是缺少JVM(Java 虚拟机(Java Virtual Machine))内存分配。最大堆大小的默认设置是 64MB 或 128MB。您可以通过设置-Xmx(-Xmx) JVM 参数来增加应用程序的最大堆大小。
例如, -Xmx512m允许为JVM分配最大 512MB 堆。要调整此参数,请右键单击“我的电脑”(My Computer)图标,然后选择“高级(Advanced)”选项卡。单击环境变量(Environment Variables)按钮:
创建CATALINA_OPTS变量,并设置所需的值,例如“ -server -Xmx256m ”。
如果您在Linux或UNIX上运行,请转到终端并通过使用sudo su – tomcat或相应的帐户名称来使用 tomcat 帐户,具体取决于您的设置。您应该使用用于运行Tomcat的帐户。
检查CATALINA_OPTS和JAVA_OPTS环境变量。将环境变量设置为更高的值。例如,如果JAVA_OPTS变量包含最小 64MB 和 128MB 最大堆大小,则将其增加到 128MB 和 256MB 值。
OLD: JAVA_OPTS="-Xms64m -Xmx128m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000"
OLD: JAVA_OPTS="-Xms128m -Xmx256m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000"
确保(Make)使用小于物理 RAM 大小的最大值,否则它将被分页到硬盘,这可能会导致更多性能问题。
要检查新设置是否生效,请转到Tomcat服务器的(Tomcat)URL,然后单击Status:
您应该会在JVM(JVM)部分下看到一个新值:
如果您在页面上没有看到更新的JVM内存,请尝试重新启动Tomcat,因为参数更改需要重新启动Tomcat才能生效。这应该可以解决您的Tomcat性能不佳的问题,并且还可以避免服务器端脚本出现内存不足的问题。如果您有任何疑问,请在评论中告诉我们。享受!
How to Increase Tomcat’s Memory Allocation
If yoυ are experiencing performance issues with Tomcat, а common cause is the lack of JVM (Jaνa Virtual Mаchine) memory allocation. The default setting of thе maximum heap size is 64MB or 128MB. Υou can іncrease the maximum heap size of applіcations by setting the -Xmx JVM parameter.
For example, -Xmx512m allows maximum 512MB heap to be allocated for the JVM. To adjust this parameter, right click the My Computer icon, then choose the Advanced tab. Click the Environment Variables button:
Create the CATALINA_OPTS variable, and set the desired value, for example “-server -Xmx256m“.
If you are running on Linux or UNIX, go to the terminal and use the tomcat account by using sudo su – tomcat or a corresponding account name depending on your setup. You should use the account that is used to run Tomcat.
Check both CATALINA_OPTS and JAVA_OPTS environment variables. Set the environment variables to a higher value. For example, if the JAVA_OPTS variable is containing a minimum of 64MB and 128MB maximum heap size, increase it to 128MB and 256MB values.
OLD: JAVA_OPTS="-Xms64m -Xmx128m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000"
OLD: JAVA_OPTS="-Xms128m -Xmx256m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000"
Make sure to use a maximum value that is less than your physical RAM’s size, otherwise it will be paged to the hard disk, which could cause more performance issues.
To check if the new settings are in effect, go to the URL of your Tomcat server and then click Status:
You should see a new value under the JVM section:
If you did not see an updated JVM memory on the page, try restarting Tomcat as parameter changes need a Tomcat restart before taking effect. This should fix your issues on poor Tomcat performance and also avoid out of memory problems with your server side scripts. If you have questions, let us know in the comments. Enjoy!