Solution to JVM memory overflow of Tomcat

Source: Internet
Author: User
Tags xms

Recently I have been familiar with a project that has been developed for several years. I need to port the database from MySQL to Oracle. First, I need to point the JDBC connection to MySQL and package it into tomcat to run it. There is no problem, however, when the JDBC connection is directed to Oracle, Tomcat continuously throws Java. lang. outofmemoryerror: I googled the Internet and learned about the running mechanism of Tomcat. I also solved the problem and shared it for future reference.

1. First, java. Lang. outofmemoryerror: Java heap Space

Explanation:

Heap size settings

JVM heap setting refers to the setting of memory space that JVM can allocate during Java program running. the JVM automatically sets the heap size value at startup. The initial space (-XMS) is 1/64 of the physical memory, and the maximum space (-xmx) is 1/4 of the physical memory. You can use options such as-xmn-XMS-xmx provided by JVM to set the configuration. The heap size is the sum of young generation and tenured generaion.
Tip: if 98% is used for GC and the available heap size is less than 2% in JVM, this exception is thrown.
Tip: the heap size should not exceed 80% of the available physical memory. Generally, you must set the-XMS and-xmx options to the same, and-xmn to the-xmx value of 1/4.

Solution:

Manually set heap size
Modify tomcat_home/bin/Catalina. BAT and add the following lines to "Echo" using catalina_base: $ catalina_base:

Java code
  1. Set java_opts = % java_opts %-server-xms800m-xmx800m-XX: maxnewsize = 256 m
[Java]View plaincopyprint?
  1. Set java_opts = % java_opts %-server-xms800m-xmx800m-XX: maxnewsize = 256 m

Or modify Catalina. Sh.
Add the following lines to "Echo" using catalina_base: $ catalina_base:
Java_opts = "$ java_opts-server-xms800m-xmx800m-XX: maxnewsize = 256 m"

2. Followed by: Java. Lang. outofmemoryerror: permgen Space

Cause:

The full name of permgen space is permanent generation space, which refers to the permanent storage area of the memory. This memory is mainly used by JVM to store class and meta information, when a class is loaded, it will be placed in the permgen space. It is different from the heap region where the class instance is stored. GC (garbage collection) permgen space is not cleaned up during the main program running period. Therefore, if your application has a class, the permgen space error may occur, this error is common when the web server pre-compile the JSP. If your web app uses a large number of third-party jar files and the size exceeds the default JVM size (4 MB), this error message is generated.

Solution:

1. manually set maxpermsize
Modify tomcat_home/bin/Catalina. BAT (Catalina. Sh in Linux ),

Java code
  1. Add the following lines to "Echo" using catalina_base: $ catalina_base:
  2. Set java_opts = % java_opts %-server-XX: permsize = 128 M-XX: maxpermsize = 512 m
[Java]View plaincopyprint?
  1. Add the following lines to "Echo" using catalina_base: $ catalina_base:
  2. Set java_opts = % java_opts %-server-XX: permsize = 128 M-XX: maxpermsize = 512 m
Add the following line to "Echo" using catalina_base: $ catalina_base ": Set java_opts = % java_opts %-server-XX: permsize = 128 M-XX: maxpermsize = 512 m

Catalina. sh:

Java code
  1. Java_opts = "$ java_opts-server-XX: permsize = 128 M-XX: maxpermsize = 512 M"
[Java]View plaincopyprint?
  1. Java_opts = "$ java_opts-server-XX: permsize = 128 M-XX: maxpermsize = 512 M"

I also saw another post and thought it was quite good. The excerpt is as follows:
Analysis of Java. Lang. outofmemoryerror: permgen Space

Many people found that the problem was caused by spring, hibernate, and tomcat. They dynamically generated classes, resulting in permanent Heap Overflow in JVM. There are different solutions. Some people say that upgrading tomcat to the latest version does not even require tomcat. Some people suspect that the issue of spring is very intense in the spring Forum, because the use of cblib in spring in AOP will dynamically generate many classes.

But the question is, why is the open source of these trump cards having the same problem? Is it a more basic reason? Tomcat answers this question in a vague manner in Q & A. We know this question, but this question is generated by a more basic question.

So someone checked the more basic JVM and found the key to the problem. In the past, sun's JVM divided the memory into different zones, one of which is the permenter area used to store a lot of classes and class descriptions. Originally sun thought that this region was fixed during JVM startup, but he did not expect that the current dynamics would be so widely used. In addition, this region has a special garbage collection mechanism. The problem now is that GC cannot be recycled after classes are dynamically loaded to this region!

For the above two problems, my solution is:

Add the following in the first line of Catalina. BAT:

Java code
  1. Set java_opts =-xms64m-xmx256m-XX: permsize = 128 M-XX: maxnewsize = 256 m-XX: maxpermsize = 256 m (I used this solution)
[Java]View plaincopyprint?
  1. Set java_opts =-xms64m-xmx256m-XX: permsize = 128 M-XX: maxnewsize = 256 m-XX: maxpermsize = 256 m

Add the following in the first line of Catalina. sh:

Java code
  1. Java_opts =-xms64m-xmx256m-XX: permsize = 128 M-XX: maxnewsize = 256 m-XX: maxpermsize = 256 m

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.