JVM memory settings

Source: Internet
Author: User

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 initial value of XMS Java heap is set to the same value as-XMS and-xmx to avoid Memory reallocation after garbage collection.

-The maximum value of xmx Java heap. The default value is 1/4 of the physical memory.

-Xmn Java heap young zone size

-Stack size of each XSS thread


Ii. JVM memory allocation settings


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 it in the eclipse root directoryEclipse. ini, The default content is (here set to runJVM memory of the current development toolAllocated ):

-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. OpenEclipse-window-preferences-Java-installed JRE(For the current developmentJava program running in the environment)

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


C. Open eclipse-run-Java application (onlyTakes effect for the set 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 ()


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.