How to Set JVM memory settings

Source: Internet
Author: User
Tags xms

This article briefly introduces several methods for JVM memory settings. When installing Java development software, the default installation includes two folders: One JDK (Java development toolbox) and one JRE (Java Runtime Environment, contains JVM), and JDK contains another JRE. if you only run the Java program, JRE is enough, and JDK is only used by developers. this section describes how to set JVM memory allocation.

A few tips on JVM memory settings

1. Set JVM memory settings

1. Four JVM memory parameters are set:

-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 allocate JVM memory settings:

(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-preference-Java-installed JRE (for all Java programs running in the current development environment)

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, edit Catalina. bat, and replace % catalina_opts % with-xmx128m-xms64m-xmn32m-xss16m.

Ii. View JVM memory settings

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 (). and configure JVM memory allocation.

Address: http://java.chinaitlab.com/advance/907896_2.html

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.