JVM memory settings (solve the out of memory problem in eclipse)

Source: Internet
Author: User
Tags xms

I. JVM memory setting principle

The default Java Virtual Machine is small in size. When processing big data, Java reports an error: Java. Lang. outofmemoryerror.
The JVM memory setting method. For a separate. class, you can use the following method to set the JVM memory for test runtime.
Java-xms64m-xmx256m Test
-XMS is used to set the memory initialization size.
-Xmx is used to set the maximum memory available (it is best not to exceed the physical memory size)

Ii. JVM memory allocation settings

1. JVM memory allocation settings have four parameters:

-The maximum value of xmx Java heap. The default value is 1/4 of the physical memory. The optimal value depends on the physical memory size and other memory overhead in the computer;

-The initial value of XMS Java heap. It is best to set-XMS and-xmx to the same value on the server JVM. The default value can be retained on the JVM of the development and testing machine;

-The size of the xmn Java heap young area. If you are not familiar with it, keep the default value;

-The stack size of each XSS thread. It is best to keep the default value if you are not familiar with it;

2. How to Set JVM memory allocation:

(1) When starting and using JVM at a command prompt (only effective for the currently running class test ):

Java-xmx128m-xms64m-xmn32m-xss16m Test

(2) When JVM is started and used in an integrated development environment (such as Eclipse:

A. Open eclipse. ini in the eclipse root directory. The default content is (the JVM memory allocation for running the current development tool is set here ):

-Vmargs
-Xms40m
-Xmx256m
-Vmargs indicates the following virtual machine setting parameters. You can modify the parameter values or add-xmn and-XSS. In addition, Eclipse. you can also set non-heap memory in ini, for example,-XX: permsize = 56 m,-XX: maxpermsize = 128 M.

The parameter values set here can be displayed in the development tool's status bar through the following Configuration:

Create the file options in the eclipse root directory. The file content is org. Eclipse. UI/perf/showheapstatus = true.

Modify the eclipse. ini file under the eclipse root directory and add the following content at the beginning:
-Debug
Options
-VM
Javaw.exe
Restart eclipse to view more JVM information in the following status.

B. Open eclipse-window-preferences-Java-installed JRE (for the JavaProgram)

Edit the currently used JRE and enter-xmx128m-xms64m-xmn32m-xss16m in the default VM parameters.

C. Open eclipse-run-Java application (only effective for the configured Java class)

Select the class-independent variable for memory allocation, and enter-xmx128m-xms64m-xmn32m-xss16m In the VM independent variable.

NOTE: If both B and C are set in the same development environment, the B setting takes effect and the c setting is invalid, for example:

The development environment is set to-xmx256m, and the test class is set to-xmx128m-xms64m. The settings that take effect when test is run are:

-Xmx256m-xms64m

(3) When JVM is started and used in a server environment (such as Tomcat) (This takes effect for the Java program in the current server environment ):

A. Set environment variables:

Variable name: catalina_opts

Variable value:-xmx128m-xms64m-xmn32m-xss16m

B. open the bin folder under the Tomcat root directory and edit Catalina. bat, in set java_opts = % java_opts % .... add: Set java_opts = % java_opts %-xms1024m-xmx1024m

 

If there is no catalina.batand there is tomcat.exe, you can right-click atat6w.exe to configure -- Java option:

-Xmx256m

-Xms64m
You can also find the Registry HKEY_LOCAL_MACHINE \ SOFTWARE \ Apache Software Foundation \ Tomcat Service Manager \ tomcat6 \ Parameters \ javaoptions.
Original Value:
-Dcatalina. Home = "C: \ apachegroup \ Tomcat 6.0"
-Djava. endorsed. dirs = "C: \ apachegroup \ Tomcat 6.0 \ common \ endorsed"
-Xrs
Join

-Xms300m

-Xmx350m
Restart the Tomcat service and the settings take effect.

 

Iii. View JVM memory information

Runtime. getruntime (). maxmemory (); // maximum available memory, corresponding to-xmx

Runtime. getruntime (). freememory (); // The current JVM idle memory.

Runtime. getruntime (). totalmemory (); // The total memory occupied by the current JVM. The value is equivalent to the total memory used by the current JVM and freemory ().

About maxmemory (), freememory () and totalmemory ():

Maxmemory () is the maximum available memory of JVM. It can be set through-xmx. The default value is 1/4 of the physical memory, and the value cannot be higher than the physical memory of the computer;

Totalmemory () is the total memory occupied by the current JVM. Its value is equivalent to the total memory used by the current JVM and the total number of freemory (). It will increase with the increase of memory usage by the JVM;

Freememory () is the idle memory of the current JVM. Because the JVM only occupies physical memory when the memory is needed, the value of freememory () is usually small, the actual available memory of JVM is not equal to freememory (), but should be equal to maxmemory ()-totalmemory () + freememory ()

Related Article

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.