Java. Lang. outofmemoryerror: Java heap space and its processing

Source: Internet
Author: User
Tags xms

Java. Lang. outofmemoryerror: Java heap space and its processing

During development, JavaProgramAn exception occurred when querying a large amount of data from the database:
Java. Lang. outofmemoryerror: Java heap Space

This exception is thrown if 98% is used for GC and the available heap size is less than 2% in JVM.

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.
Example: Java-jar-xmn16m-xms64m-xmx128m MyApp. Jar
If the heap size is set too small, in addition to the exception information, the response speed of the program will also be slow. GC takes more time, and the execution time assigned by the application is less.
The heap size should not exceed 80% of the available physical memory. Generally, the-XMS and-xmx options must be set to the same, and-xmn is the-xmx value of 1/4.
The-XMS-xmn setting of heap size should not exceed the physical memory size. Otherwise, the system prompts "error occurred during initialization of VM cocould not reserve enough space for object Heap ".

The root cause of this problem is that the default heap size of the JVM virtual machine is 64 MB, which can be achieved by setting the maximum and minimum values. There are several ways to set the heap size.

1. You can change the system environment variables in windows.
Add java_opts =-xms64m-xmx512m

2. If Tomcat is used, in windows

C:/tomcat5.5.9/bin/Catalina. BAT:
Set java_opts =-xms64m-xmx256m

The location is added to the row REM guess catalina_home if not defined.

3. For a Linux System
Before {tomcat_home}/bin/Catalina. Sh, add
Set java_opts = '-xms64-xmx512'

 

It should be noted that in the release environment, it is feasible to configure according to the above method. For example, in the development environment, other factors are still being considered.

 

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.