Solve Tomcat memory overflow

Source: Internet
Author: User
Tags xms

Console error message:

Java. Lang. outofmemoryerror: Java heap Space

 

 

Solution:

 

If you use startup. BAT to start Tomcat (that is, the green version is used)

Directly add set java_opts =-xms128m-xmx350m to the front of Catalina. bat.

Restart the Tomcat Service

 

If it is an installation version (it is best to use green and environmentally friendly)

Modify the Registry HKEY_LOCAL_MACHINE/software/Apache Software Foundation/tomcat Service Manager/tomcat5/parameters/javaoptions
Original Value:
-Dcatalina. Home = "C:/apachegroup/tomcat 5.0"
-Djava. endorsed. dirs = "C:/apachegroup/tomcat 5.0/common/endorsed"
-Xrs

Add-xms300m-xmx350m
Restart the Tomcat Service

 

In the above method, pay more attention to the memory processing when writing code.

 

Check the current JVM memory usage:

System. Out. println ("JVM Max memory:" + runtime. getruntime (). maxmemory ()/1024/1024 + "M ");
System. Out. println ("JVM is using memory:" + runtime. getruntime (). totalmemory ()/1024/1024 + "M ");
System. Out. println ("JVM is free memory:" + runtime. getruntime (). freememory ()/1024/1024 + "M ");

All three methods are about JVM memory usage rather than the operating system memory;

The maxmemory () method returns the maximum memory that the Java Virtual Machine (this process) can dig from the operating system, in bytes. If you are running a Java program, if the-xmx parameter is not added, it is 64 mb. That is to say, maxmemory () returns about 64*1024*1024 bytes, this is the maximum memory that the Java Virtual Machine can dig from the operating system by default. If the-xmx parameter is added, the value following this parameter prevails. For example, Java-CP classpath-xmx512m classname, the maximum memory size is 512*1024*0124 bytes. The totalmemory () method returns the memory size that the Java Virtual Machine has dug from the operating system, that is, all the memory occupied by the Java Virtual Machine process at that time. If the-XMS parameter is not added when running Java, the memory is always slowly dug from the operating system during the Java program running process, basically how much is used to dig, until maxmemory (), totalmemory () increases slowly. If the-XMS parameter is used, the program will unconditionally dig the memory size defined after-XMS from the operating system at startup, and then when the memory usage is similar, then dig. What is freememory ()? If the-XMS parameter is not added while running Java, the process of running Java is as follows, the memory is always dug slowly from the operating system, basically the number of resources to be dug, but the Java Virtual Machine will dig a little more in 100% cases, the memory is actually freememory (), so the value of freememory () is usually very small, however, if you use-XMS when running a Java program, this is because the program will unconditionally dig out the number of memory defined after the XMS from the operating system at startup, at this time, most of the dug memory may be useless, so freememory () may be a little large at this time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.