java-jvm-Heap Memory "three"

Source: Internet
Author: User

Heap Memory Setting principle

JVM heap memory is divided into 2 blocks: Permanent space and heap space.

    • Permanent is the persistent generation (Permanent Generation), which mainly holds Java class definition information, which is not related to the Java objects that the garbage collector collects.

    • Heap = {old + new = {Eden, ' from ', to}},old is the older Generation, NEW is the young Generation (Generation). The division of old generations and younger generations has a greater impact on garbage collection.

Young generation

All newly generated objects are first placed in the young generation. The goal of the young generation is to collect as quickly as possible those objects with short life cycles. The young generation generally divided into 3 districts, 1 Eden Districts, 2 survivor districts (from and to).

Most objects are generated in the Eden area. When the Eden Zone is full, the surviving objects will be copied to the Survivor area (one of two), and when a survivor area is full, the surviving objects of this area will be copied to another survivor area, and when another survivor area is full, Objects that were copied from a former survivor area and survived at this time may be copied to the old generation.

2 survivor zones are symmetrical and have no succession, so the same survivor area may have objects copied from the Eden area and copied from the other survivor area, while the ones copied to the old quarter only come from another survivor area. also, at least one of the survivor areas is empty because of the need for Exchange . In special cases, depending on the program's needs, the survivor area can be configured to multiple (more than 2), which increases the age of the object in the younger generation and reduces the likelihood of being placed in the old generation.

Garbage collection for younger generations is the young GC.

Old generation

Objects that survived after N (configurable) garbage collection in the young generation are copied into the old generation. Therefore, it can be considered that older generations are storing objects with longer life cycles.

Garbage collection for older generations is the full GC.

Durable generation

Used to store static type data, such as Java Class, Method, and so on. Persistent generations have no significant impact on garbage collection. However, some applications may dynamically generate or invoke some classes, such as Hibernate CGLib, where it is often necessary to set a large, persistent generation space to store dynamically increasing types during these runs.

So, when a group of objects are generated, the memory request process is as follows:

    1. The JVM tries to initialize an area of memory for the associated Java object in the young generation's Eden area.

    2. When the Eden space is sufficient, the memory request ends. Otherwise, proceed to the next step.

    3. The JVM tries to free all inactive objects in the Eden area (young GC). Once released, if Eden Space is still not enough to fit into the new object, the JVM attempts to put some of the active objects in the Eden area into the survivor area.

    4. The survivor area is used as an intermediate swap area for the Eden area and the old generation. When the old generation of space is sufficient, the number of objects that have survived in the survivor area will be moved to the old generation.

    5. When there is not enough space in the old generation, the JVM performs a complete garbage collection (full GC) in the older generation.

    6. Full GC, if the survivor and older generations are still unable to store objects copied from the Eden Zone, the JVM will not be able to request memory for the newly generated object in the Eden area, i.e. "out of memory".

OOM ("Out of Memory") exceptions are generally mainly for the following 2 reasons :

1. Old generation overflow, performance: Java.lang.OutOfMemoryError:Javaheapspace

This is the most common scenario, which can be caused by setting the memory parameter Xmx too small or program memory leaks and improper use.

For example, iterate over tens of thousands of string processing, create thousands of objects, and request hundreds of m or even upper g of memory within a piece of code. There are times when the memory overflow is not reported, but it will make the system uninterrupted garbage collection, and can not handle other requests. In this case, in addition to checking procedures, print heap memory and other methods to troubleshoot, but also with some memory analysis tools, such as Mat is very good.


2. Persistent overflow, performance: Java.lang.OutOfMemoryError:PermGenspace

Usually due to the persistence of the set too small, dynamic loading of a large number of Java classes resulting in overflow, the solution is only the parameter-xx:maxpermsize (General 256m to meet the overwhelming majority of application requirements). The way to load some Java classes into a container share (such as Tomcat share lib) is also a thought, but only if multiple applications are deployed in the container, and these applications have a large number of shared class libraries.

Parameter description
  • -xmx3550m: Set JVM maximum heap memory to 3550M.

  • -xms3550m: Set the JVM initial heap memory to 3550M. This value can be set to the same as-xmx to avoid the JVM reallocating memory after each garbage collection completes.

  • -xss128k: Sets the stack size for each thread . After JDK5.0, each thread stack size is 1M, before each thread stack size is 256K. Adjustments should be made based on the size of memory required by the application's thread. 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. It is important to note that when this value is set to a larger size (for example, >2MB), the performance of the system will be greatly reduced.

  • -XMN2G: Set the young generation size to 2G. In the case where the entire heap memory size is determined, increasing the younger generation will reduce the age generation and vice versa. This value is related to JVM garbage collection, which has a large impact on system performance and is officially recommended for 3/8 of the entire heap size.

  • -xx:newsize=1024m: Sets the initial value of the young generation to 1024M.

  • -xx:maxnewsize=1024m: Sets the maximum value of the young generation to 1024M.

  • -xx:permsize=256m: Set the persistent generation initial value to 256M.

  • -xx:maxpermsize=256m: Sets the maximum value for the persistent generation to 256M.

  • -xx:newratio=4: Set the ratio of the younger generation (including 1 Eden and 2 survivor) to the old generation. Says the younger generation is 1:4 more than old.

  • -xx:survivorratio=4: Sets the ratio between Eden and survivor in the younger generation . Represents 2 survivor Zones (the default in the JVM heap memory for 2 equal-sized survivor areas) and 1 Eden areas is 2:4, or 1 survivor extents represent the entire young generation of 1/6.

  • -xx:maxtenuringthreshold=7: Indicates an object that has not been recycled into the old generation if it has been moved 7 times in the Survivor area (salvage space). If set to 0, then the young generation object does not go through the survivor area, directly into the old generation, for applications that require a large number of resident memory, this can improve efficiency. If you set this value to a larger value, the younger generation objects will replicate multiple times in the Survivor area, which increases the chance that an object can survive in the younger generation, increase the probability that the object will be garbage collected in the younger generation, and reduce the frequency of the full GC, which can improve service stability to some extent.

Questions and Answers

-xmn,-xx:newsize/-xx:maxnewsize,-xx:newratio 3 sets of parameters can affect the size of the younger generation, in the case of mixed use, what is the priority?

As follows:

    1. High-priority:-xx:newsize/-xx:maxnewsize

    2. Medium Priority:-xmn (default equivalent-xmn=-xx:newsize=-xx:maxnewsize=?)

    3. Low-priority:-xx:newratio

The-XMN parameter is recommended because the parameter is concise, equivalent to setting the newsize/maxnewsize once, and the two are equal for the production environment. -xmn with-XMS/-XMX, you can complete the heap memory layout.

The-XMN parameter is supported at the beginning of JDK 1.4.

Original address: https://my.oschina.net/u/175660/blog/351774

java-jvm-Heap Memory "three"

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.