JVM (Java Virtual machine) memory settings

Source: Internet
Author: User
Tags xms

first, set the JVM memory settings

1. There are four parameters for setting JVM memory:

-xmx Java Heap Maximum, the default value is 1/4 of physical memory, the best setting should depend 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 tester JVM can retain the default value;

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

-XSS the stack size of each thread, not familiar with the best to keep the default value;

2. How to set the JVM memory allocation:

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

java-xmx128m-xms64m-xmn32m-xss16m Test

(2) When you start and use the JVM in an integrated development environment, such as Eclipse:

A. Open Eclipse.ini at the eclipse root, with the default content (here is the JVM memory allocation running the current development tool):

    1. -vmargs
    2. -xms40m
    3. -xmx256m

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

The parameter values set here can be displayed in the development tool status bar using the following configuration:

Create the file options under the Eclipse root directory with the file content: Org.eclipse.ui/perf/showheapstatus=true

Modify the Eclipse.ini file in the Eclipse root directory by adding the following at the beginning:

    1. -debug
    2. Options
    3. -vm
    4. Javaw.exe

Restart Eclipse and you can see that the status bar below has more JVM information.

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

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

C. Open eclipse-Run-run-java application (only valid for the Java classes you set)

Select the class-independent variable that you want to set the memory allocation to, and enter it in the VM argument:-xmx128m-xms64m-xmn32m-xss16m

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

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

-xmx256m-xms64m

(3) When the JVM is started and used in a server environment (such as Tomcat) (the Java program is in effect for 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 the Catalina.bat. talina_opts% (total around) replaced by:-xmx128m-xms64m-xmn32m-xss16m

second, view set JVM memory information

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

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

Runtime.getruntime (). TotalMemory (); The total amount of memory occupied by the current JVM, which is equivalent to the sum of the memory used by the current JVM and the Freememory ()

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

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

TotalMemory () is the total amount of memory occupied by the current JVM, which is equal to the sum of the memory used by the current JVM and the Freememory (), which increases as the JVM uses memory;

Freememory () is the current JVM's free memory, because the JVM consumes physical memory only when it needs memory, so the value of freememory () is generally small, and the actual available memory of the JVM is not equal to Freememory (), Instead, it should be equal to MaxMemory ()-totalmemory () +freememory (). and set JVM memory allocations.

Official documents See:

Http://docs.sun.com/source/819-0084/pt_tuningjava.html

Java startup parameters are divided into three categories;
The first is the standard parameter (-), all JVM implementations must implement the functions of these parameters, and backward compatibility;
The second is the non-standard parameter (- x), the default JVM implements the functions of these parameters, but does not guarantee that all JVM implementations are satisfied, and does not guarantee backward compatibility;
The third is the non-stable parameter (-XX), such parameters of each JVM implementation will be different, in the future may be canceled at any time, need to use cautiously;

The standard parameters are more useful:

verbose
-verbose:class

Outputs information about the JVM loading classes that can be diagnosed when the JVM reports that a class or class violation is not found.
-VERBOSE:GC
Outputs the correlation of each GC.
-verbose:jni
The output native method invocation is typically used to diagnose JNI invocation error messages.

non-standard parameters are also known as extended parameters

The most common use is

-XMS512m sets the JVM to drive memory to 512m. This value can be set to the same as-xmx to avoid the JVM reallocating memory after each garbage collection completes.

-Xmx512m, set the JVM's maximum available memory to 512M.

-xmn200m: set the young generation size to 200M. The entire heap size = younger generation size + old generation size + persistent generation size. The permanent average fixed size is 64m, so increasing the younger generation will reduce the size of older generations. This value has a large impact on system performance, and Sun's official recommendation is 3/8 for the entire heap.

-XSS128k:

Sets the stack size for each thread. After JDK5.0, each thread has a stack size of 1M, before each thread has a stack size of 256K. The size of the memory required for the more applied threads to be adjusted. In the same physical memory, reducing this value can generate more threads. However, the operating system of the number of threads within a process is still limited, can not be generated indefinitely, the empirical value of 3000~5000 around.

 

-xloggc:file
similar to the-VERBOSE:GC feature, just log the correlation of each GC event to a file, preferably locally, to avoid potential problems with the network.
If the verbose command appears on the command line at the same time, the-XLOGGC will prevail.
-xprof

Keep track of running programs and trace data in standard output, suitable for development environment debugging.

The list of parameters prefixed with-XX may not be robust in the JVM, Sun is not recommended, and may be canceled without notice, but many of these parameters are useful to us, such as the-xx:permsize and-xx that we often see: MaxPermSize and so on;

First, let's introduce the behavior parameters :

limits the amount of time the JVM spends on GC before throwing an oom
parameter and its default value description
-xx:-disableexplicitgc Disable Calling Syst EM.GC (); But the JVM's GC is still valid
-xx:+maxfdlimit maximize the limit on the number of file descriptors
-XX:+SCAVENGEBEFOREFULLGC Cenozoic GC takes precedence over full GC execution
-xx:+usegcoverheadlimit
-xx:-useconcmarksweepgc on old Generation using concurrent Tag Exchange algorithm for GC
-xx:-useparallelgc enable parallel gc
-xx:-useparalleloldgc enables parallelism on full GC, which is automatically enabled when-XX:-USEPARALLELGC is enabled
-xx:-useserialgc enable serial GC
-xx:+usethreadpriorities Enable local thread priority

The three parameters of the blackbody in the table above represent three ways that the GC executes in the JVM, namely serial, parallel, concurrency ;
Serial (SERIALGC) is the default GC mode of the JVM, which is generally suitable for small applications and single processors, the algorithm is simple and the GC is more efficient, but it may bring pause to the application.
Parallel (PARALLELGC) refers to the GC runtime, no impact on the application run, GC and app both threads are executing concurrently, so as to minimize the impact of the app operation;
Concurrency (CONCMARKSWEEPGC) is a multi-thread concurrent execution GC, generally applicable to multiprocessor systems, can improve the efficiency of GC, but the algorithm is complex, the system consumes large;

Performance Tuning parameter list:

parameters and their default values description
-xx:largepagesizeinbytes=4m settings with Large page size for Java heap
-xx:maxheapfreeratio=70 GC after the maximum amount of idle in the Java heap
-xx:maxnewsize=size New Build objects can occupy a maximum memory value
-xx:maxpermsize=64m Laosheng objects can occupy the maximum memory value
-xx:minheapfreeratio=40 GC The minimum percentage of idle in the Java heap
-xx:newratio=2 ratio of cenozoic memory capacity to Laosheng memory capacity
-xx:newsize=2.125m The default value of memory used when generating new generation objects
-xx:reservedcodecachesize=32m preserves the amount of memory the code uses
-xx:threadstacksize=512 Sets the thread stack size, using the system default value if 0
-xx:+uselargepages using large page memory

In the daily performance tuning we will basically use the above properties of the blackbody;

Debug Parameter list:

Parameters and their default values Describe
-xx:-citime When the print consumption is JIT-compiled
-xx:errorfile=./hs_err_pid<pid>.log Save the error log or data to a file
-xx:-extendeddtraceprobes To open a Solaris-specific dtrace probe
-xx:heapdumppath=./java_pid<pid>.hprof specifies the path or file name when the heap information is exported
-xx:-heapdumponoutofmemoryerror Export the information in this heap when an oom is first encountered
-XX: Run the custom command after a fatal error occurs
-xx:onoutofmemoryerror= "<cmd args>;<cmd args>" Execute a custom command when an oom is first encountered
-xx:-P Rintclasshistogram Print the column information of the class instance after encountering Ctrl-break, same as the Jmap-histo function
-xx:-P rintconcurrentlocks Print concurrency lock information after encountering Ctrl-break, same as jstack-l function
-xx:-P Rintcommandlineflags Print markup that appears on the command line
-xx:-P rintcompilation When a method is compiled, the relevant information is printed
-xx:-P RINTGC Print related information each time the GC
-xx:-P RINTGC Details Print Details per GC
-xx:-P Rintgctimestamps Print timestamp for each GC
-xx:-traceclassloading Tracking load information for a class
-xx:-traceclassloadingpreorder Trace load information for all classes referenced to
-xx:-traceclassresolution TRACE Constant Pool
-xx:-traceclassunloading Tracking uninstall information for a class
-xx:-traceloaderconstraints Tracking information about class loader constraints

JVM (Java Virtual machine) memory settings

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.