Java stack memory heap memory and GC related

Source: Internet
Author: User
Tags xms

Java stack Memory heap

Java divides memory into two kinds, one called stack memory, and the other is called Heap memory, which has different functions. Stack memory is used to store local variables and method calls.
Stack memory belongs to a single thread, each thread will have a stack of memory, its stored variables can only be seen in its own thread, that is, stack memory can be understood as a thread of private memory.
Objects in the heap memory are visible to all threads. Objects in the heap memory can be accessed by all threads. Heap memory is used to store objects in Java. Whether they are member variables, local variables, or class variables, the objects they point to are stored in heap memory.

A reference variable is a normal variable that is defined when memory is allocated in the stack, and the reference variable is released in the program run to the extraterritorial scope. The array and object itself is allocated in the heap, and even if the program runs beyond the block of code that uses new to produce arrays and objects, the heap memory used by the array and the object itself is not freed, and the groups and objects become garbage and can no longer be used when no reference variable points to it. , but still occupies memory and is released by the garbage collector at a later indeterminate time. This is also the main reason for the memory of Java comparison, in fact, the variables in the stack point to the heap memory variables, this is the pointer in Java!

JAVAGC and Tuning

Java's garbage collection mechanism is performed by GC threads, which are the daemon threads used by the Java Virtual machine itself. Responsible for cleaning up the so-called "unreachable" objects. When the program creates an object, the GC starts to monitor the object's address, size, and usage. When the GC determines that some objects are unreachable, it is the responsibility of the GC to reclaim those memory spaces.

Refer to the following list of common parameters of the JVM for tuning

Parameter name Meaning Default value
-xms Initial Heap Size 1/64 of physical Memory (<1GB) The default (minheapfreeratio parameter can be adjusted) when the free heap memory is less than 40%, the JVM increases the heap until the maximum limit of-xmx.
-xmx Maximum Heap Size Physical memory of the quarter (<1GB) The default (maxheapfreeratio parameter can be adjusted) when the free heap memory is greater than 70%, the JVM reduces the heap until the minimum limit of-XMS
-xmn Young generation size (1.4or lator) Note : The size here is (eden+ 2 survivor space). It is different from the new Gen shown in Jmap-heap.
The entire heap size = younger generation size + old generation size + persistent generation size.
Increasing the younger generation will reduce the size of older generations. This value has a large impact on system performance, and Sun is the official recommended configuration for the entire heap of 3/8
-xx:newsize Set the young generation size (for 1.3/1.4)
-xx:maxnewsize Young generation Max (for 1.3/1.4)
-xx:permsize Set the persistence generation (Perm Gen) initial value 1/64 of physical memory
-xx:maxpermsize Set Persistent generation maximum value 1/4 of physical memory
-xss 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 thread to be applied is adjusted. In the same physical memory, reducing this value can generate more threads. However, the operating system has a limit on the number of threads in a process and cannot be generated indefinitely. Experience value is around 3000~5000
Generally small applications, if the stack is not very deep, should be 128k enough for large applications recommended using 256k. This option has a large impact on performance and requires rigorous testing. Principal
Similar to the Threadstacksize option interpretation, the official documentation does not seem to explain the phrase in the Forum: ""
-XSS is translated in a VM flag named Threadstacksize "
The general setting of this value is available.
-xx:threadstacksize Thread Stack Size (0 means use default stack size) [sparc:512; Solaris x86:320 (was-prior in 5.0 and earlier); Sparc bit:1024; Linux amd64:1024 (was 0 in 5.0 and earlier); All others 0.]
-xx:newratio Ratio of young generations (including Eden and two survivor districts) to older generations (excluding persistent generations) -xx:newratio=4 says the ratio of young generations to older generations is 1:4, and the younger generation accounts for 1/5 of the entire stack.
Xms=xmx and the xmn is set, this parameter does not need to be set.
-xx:survivorratio The ratio of the Eden area to the survivor area Set to 8, the ratio of two survivor to one Eden area is 2:8, and a survivor area represents 1/10 of the entire young generation.
-xx:largepagesizeinbytes The size of the memory page cannot be set too large, affecting the size of the perm =128m
-xx:+usefastaccessormethods Rapid optimization of primitive types
-xx:+disableexplicitgc Close System.GC () This parameter requires rigorous testing.
-xx:maxtenuringthreshold Maximum age of garbage If set to 0, then the young generation object does not go through the survivor area, directly into the old generation. For older generations of more applications, can improve efficiency. If you set this value to a larger value, the younger generation object will replicate multiple times in the Survivor area, which increases the lifetime of the object's younger generations, increasing the probability of being recycled in the younger generation.
This parameter is valid only when the serial GC is in effect.
-xx:+aggressiveopts Faster compilation
-xx:+usebiasedlocking Performance improvement of lock mechanism
-xnoclassgc Disable garbage collection
-xx:softreflrupolicymspermb SoftReference survival time in free space per megabyte of heap 1s Softly reachable objects would remain alive for some amount of time after the last time they were referenced. The default value is one second of lifetime per free megabyte in the heap
-xx:pretenuresizethreshold Objects more than how large are directly allocated in the old generation 0 Unit bytes Invalid When using parallel scavenge GC for Cenozoic
Another case that is allocated directly in the old generation is a large array object, and there are no externally referenced objects in the array.
-xx:tlabwastetargetpercent Tlab% of Eden area 1%
-xx:+Collectgen0first Whether to YGC first when FULLGC False

Parallel Collector related Parameters

-xx:+useparallelgc Full GC with parallel MSC
(This is to be verified)

Select the garbage collector as the parallel collector. This configuration is valid only for younger generations. That is, the younger generation uses concurrent collection, while older generations still use serial collection. (This is to be verified)

-xx:+useparnewgc Set Young on behalf of the parallel collection Can be used together with CMS collection
JDK5.0 above, the JVM will set itself according to the system configuration, so it is no longer necessary to set this value
-xx:parallelgcthreads Number of threads in the parallel collector This value is best configured equal to the number of processors equally applicable to the CMS
-xx:+useparalleloldgc Old Generation garbage collection method for parallel collection (Parallel compacting) This is the parameter option that appears in Java 6
-xx:maxgcpausemillis Maximum time for each young generation of garbage collection (max pause time) If this time is not met, the JVM automatically adjusts the young generation size to meet this value.
-xx:+useadaptivesizepolicy Automatic selection of the size of the young generation and corresponding survivor area ratio When this option is set, the parallel collector automatically selects the size of the young generation area and the corresponding Survivor area scale to achieve the minimum corresponding time specified by the target system, or the collection frequency, which is recommended to be turned on when using the parallel collector.
-xx:gctimeratio Set garbage collection time as a percentage of program run time The formula is 1/(1+n)
-xx:+SCAVENGEBEFOREFULLGC Call YGC before full GC True Do young generation GC prior to a full GC. (introduced in 1.4.1.)

Java stack memory heap memory and GC related

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.