Java.lang.OutOfMemoryError:Java Heap Space Solution

Source: Internet
Author: User

Recently in the familiar with a project that has been developed for several years, the need to transfer the database from MySQL to Oracle, the first point of the JDBC connection to MySQL, packaged into Tomcat inside, can run, no problem, but when the JDBC connection point to Oracle, Tomcat on the continuous throw java.lang.OutOfMemoryError error, internet Google a bit, understand the operation of Tomcat, also solve the problem, share out, for reference.

1, the first is: Java.lang.OutOfMemoryError:Java heap space

Explanation:

Heap Size Setting

The JVM heap setting is the set of memory space that the JVM can provision during the run of the Java program. The JVM automatically sets the value of the heap size when it starts, and its initial space (that is,-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 the-XMN-XMS-XMX provided by the JVM to set it up. The size of Heap size is the sum of young Generation and tenured generaion.
Tip: This exception message is thrown in the JVM if 98% of the time is used for GC and the available heap size is less than 2%.
Tip: The Heap Size does not exceed 80% of the available physical memory, generally the-XMS and-XMX options are set to the same, and the-XMX value of-xmn is 1/4.

Workaround:

Manually setting the heap size
To modify the Tomcat_home/bin/catalina.bat, add the following line above the "echo" Using catalina_base: $CATALINA _base "":
Set java_opts=%java_opts%-server-xms800m-xmx800m-xx:maxnewsize=256m

or modify catalina.sh
Add the following line to the "echo" Using catalina_base: $CATALINA _base "":
java_opts= "$JAVA _opts-server-xms800m-xmx800m-xx:maxnewsize=256m"

2, followed by: Java.lang.OutOfMemoryError:PermGen space

Reason:

PermGen space is the full name of permanent Generation space, refers to the memory of the permanent storage area, this block of memory is mainly stored by the JVM class and meta information, class is loader will be placed in PermGen Space, unlike the heap area where the class instance (Instance) is stored, the GC (garbage Collection) does not clean up permgen space during the main program run time, so if you have a class in your application, PermGen space errors are most likely to occur when the Web server pre-compile the JSP. If you have a large number of third-party jars under your web app that are larger than the JVM's default size (4M), this error message will be generated.

Workaround:

1. Manually set the MaxPermSize size
Modify the Tomcat_home/bin/catalina.bat (under Linux for catalina.sh) and add the following line on the "echo" Using catalina_base: $CATALINA _base "":
Set java_opts=%java_opts%-server-xx:permsize=128m-xx:maxpermsize=512m

Under catalina.sh:
java_opts= "$JAVA _opts-server-xx:permsize=128m-xx:maxpermsize=512m"


In addition to see another post, feel very good, excerpt as follows:
Analysis Java.lang.OutOfMemoryError:PermGen Space

It is found that many people attribute the problem to: Spring,hibernate,tomcat because they dynamically produce classes that cause the permanent heap overflow in the JVM. Then there are a few opinions about how to upgrade the Tomcat version to the newest or even simply without Tomcat. There are also doubts about the spring issue, which is hotly debated on the Spring Forum, as spring uses cblib in AOP to generate many classes dynamically.

But the question is why the same problem arises from the open source of these trump cards, is it a more basic reason? Tomcat in Q&a very vague answer to this, we know this problem, but this problem is caused by a more basic problem.

Then someone checked the more basic JVM and found the key to the problem. It turns out that the Sun's JVM has divided the memory into different areas, one of which is the Permenter area used to store very many classes and class descriptions. Sun was designed to think the area was fixed when the JVM started, but he didn't expect it to be used so extensively. And this area has a special garbage recovery mechanism, now the problem is that the dynamic loading class to this area, the GC has no way to recycle!


for the above two questions, my handling is:

The first line in Catalina.bat is incremented:
Set java_opts=-xms64m-xmx256m-xx:permsize=128m-xx:maxnewsize=256m-xx:maxpermsize=256m

The first line in catalina.sh is incremented:
java_opts=-xms64m-xmx256m-xx:permsize=128m-xx:maxnewsize=256m-xx:maxpermsize=256m

Java.lang.OutOfMemoryError:Java Heap Space resolution

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.