JVM Memory allocation settings

Source: Internet
Author: User

When installing Java development software, the default installation consists of two folders, one JDK (Java Development Toolbox), one JRE (Java running environment, with JVM), where a JRE is included in the JDK. If you are only running Java programs, the JRE is sufficient, and the JDK is for developers only.
One, JVM memory allocation settings

1. There are four parameters for JVM memory allocation settings:

-xmx Java Heap Maximum, the default value is 1/4 of physical memory, the best set value should be depending on the physical memory size and other memory overhead in the computer;

-xms Java Heap Initial value, the server-side JVM is best to set-XMS and-xmx to the same value, the development of the test machine JVM can retain the default value;

-xmn Java Heap Young area size, not familiar with the best to retain the default value;

-XSS the stack size of each thread, it's best to keep the default value;

2. How to set the JVM's memory allocation:

(1) When the JVM is started and used at a command prompt (only the currently running class Test takes effect):

java-xmx128m-xms64m-xmn32m-xss16m Test

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

A. Open Eclipse.ini under the Eclipse root, default (here is the JVM memory allocation for running the current development tool):

-vmargs
-xms40m
-xmx256m
-vmargs indicates that the following set parameters for the virtual machine, you can modify the parameter values, you can also add-xmn,-xss, in addition, Eclipse.ini can also set the non heap memory, such as:-xx:permsize=56m,-xx:maxpermsize=128m.

The parameter values that you set here can be displayed in the status bar of the development tool with the following configuration:

To create the file options in the Eclipse root directory, the contents of the file are: org.eclipse.ui/perf/showheapstatus=true

Modify the Eclipse.ini file in the Eclipse root directory and add the following at the beginning:
-debug
Options
-vm
Javaw.exe
When you restart Eclipse, you can see the JVM information in the lower State bar.

B. Open eclipse-Window-Preferences-java-installed JRE (effective for Java programs running in the current development environment)

Edit the currently used JRE and enter it in the default VM parameter:-xmx128m-xms64m-xmn32m-xss16m

C. Open eclipse-run-run the-java application (only for Java classes that are set to take effect)

Select the class to set the memory allocation-the argument, and enter it in the VM's argument:-xmx128m-xms64m-xmn32m-xss16m

Note: If both the B and C settings are in place in the same development environment, the B setting takes effect and the C setting is not valid, such as:

The development environment is set to:-xmx256m, and Class test is set to:-xmx128m-xms64m, the setting that takes effect when you run test is:

-xmx256m-xms64m

(3) When starting and using the JVM in a server environment (such as Tomcat) (the Java program takes effect for the current server environment):

A. Setting environment variables:

Variable name: catalina_opts

Variable Value:-xmx128m-xms64m-xmn32m-xss16m

B. Open the Bin folder in the Tomcat root directory, edit the Catalina.bat, and replace%catalina_opts% (all around) with:-xmx128m-xms64m-xmn32m-xss16m

Second, view the JVM memory information

Runtime.getruntime (). MaxMemory (); Maximum available memory, corresponding to-XMX

Runtime.getruntime (). Freememory (); Current JVM free memory

Runtime.getruntime (). TotalMemory (); Total memory consumed by the current JVM, with a value equal to the total memory used by the current JVM and Freememory ()

About MaxMemory (), Freememory () and TotalMemory ():

MaxMemory () is the maximum available memory for the JVM, which can be set by the-XMX setting, with a default value of 1/4 of the physical memory and not higher than the physical memory of the computer;

TotalMemory () The total amount of memory consumed by the current JVM, the value of which is the sum of the Memory and Freememory () used by the current JVM, and increases as the JVM uses more memory;

Freememory () is an idle memory for the current JVM, because the JVM consumes physical memory only when memory is needed, so the value of freememory () is generally small, and the actual memory available to the JVM is not equal to Freememory (). Instead, it 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.