Java memory allocation and GC, Java memory allocation GC

Source: Internet
Author: User
Tags apache log xms

Java memory allocation and GC, Java memory allocation GC

Java memory allocation and recovery mechanisms are summarized as follows: generational allocation and generational collection.

Objects are divided into Young Generation, Old Generation, and Permanent Generation Based on the survival time ).

The memory space occupied by each new object in java is the space of the young generation. After the java garbage collection mechanism recycles Resources in the heap area, those resources that were not recycled in the young generation will be transferred to the Middle Ages, and those in the middle ages will be transferred to the old generation.

Memory is composed of Perm and Heap.
Heap = {Old + NEW = {Eden, from, }}
In the JVM memory model, Heap is divided into two major parts: NEW Generation (young Generation) and Old Generation (Old Generation ).
In New Generation, there is a space called Eden, mainly used to store New objects, and there are two consumer vor Spaces (from, ), they are used to store objects that survive each garbage collection.
In Old Generation, memory objects with long lifecycles are stored in applications.
Permanent Generation is mainly used to put JVM reflection objects, such as class objects and method objects.

In NewGeneration blocks, the Copying algorithm is generally used for garbage collection, which is fast. During each GC, the surviving objects are first copied from Eden to a certain sequence vorspace. When the sequence vor Space is full, the remaining live objects are directly copied to OldGeneration. Therefore, after each GC, the Eden memory block is cleared. In the OldGeneration block, the mark-compact algorithm is generally used for garbage collection, which is slower, but reduces memory requirements.
Garbage collection is divided into multiple levels. garbage collection in the OLD section will be collected when the level 0 is Full. garbage collection in the Level 1 or above will only be collected in the NEW section, memory overflow usually occurs after garbage collection of the OLD or Perm segments, and there is still no memory space to accommodate new Java objects.

When a URL is accessed, the memory application process is as follows:
A. JVM will try to initialize A memory area for the relevant Java object in Eden
B. When the Eden space is sufficient, the memory application is completed. Otherwise, go to the next step.
C. JVM tries to release all inactive objects in Eden (which belongs to 1 or more advanced garbage collection). If the Eden space is not enough to put new objects, try to put some active objects in Eden into the same vor Area
D. The primary vor area is used as the intermediate swap area of Eden and OLD. When the OLD area has sufficient space, objects in the primary vor area will be moved to the Old area. Otherwise, objects in the primary vor area will be retained.
E. When there is not enough space in the OLD area, JVM will perform full garbage collection in the OLD area (Level 0)
F. after full garbage collection, if the primary VOR and OLD areas still cannot store some objects copied from Eden, the JVM cannot create a memory area for the new object in the Eden area, "out of memory error" appears"

JVM memory parameters:
Configure heap parameters: (-Xms,-Xmx,-XX: newSize,-XX: MaxnewSize,-Xmn)
1.-Xms: indicates the size of initial memory allocation in the heap area of the Java Virtual Machine, which is usually 1/64 of the available memory of the operating system, but still needs to be allocated according to the actual situation.
2.-Xmx: indicates the maximum memory size that can be allocated in the heap area of the Java Virtual Machine (JVM), which is usually 1/4 of the available memory size of the operating system. However, during development, the configuration of-Xms and-Xmx parameters is usually the same, the purpose is to waste resources so that the size of the heap area does not need to be re-separated after the java garbage collection mechanism clears the heap area.
-XX: newSize,-XX: MaxnewSize, and-Xmn are further subdivided into the heap areas: the new generation, the middle generation, and the old generation. The memory space occupied by each new object in java is the new generation space. After the java garbage collection mechanism recycles Resources in the heap area, those resources that are not recycled in the new generation will be transferred to the Middle Ages, and those in the middle ages will be transferred to the old generation. The following three parameters are used to control the memory size of the new generation.
1.-XX: newSize: the initial memory size of the new generation, which should be smaller than the value of-Xms;
2.-XX: MaxnewSize: indicates the maximum memory size that can be allocated to the new generation. Of course, this value should be smaller than the value of-Xmx;
3.-Xmn: As for this parameter, both-XX: newSize and-XX: MaxnewSize parameters are configured simultaneously. That is to say, if-Xmn is used to configure the memory size of the new generation, -XX: newSize =-XX: MaxnewSize =-Xmn. Although it is very convenient, note that this parameter is used only after JDK1.4.
Configure non-heap parameters: (-XX: PermSize,-XX: MaxPermSize)
1.-XX: PermSize: indicates the initial memory allocation size for non-heap areas. Its abbreviation is permanent size (persistent memory)
2.-XX: MaxPermSize: indicates the maximum memory size allocated to non-heap areas.
Note that the non-heap memory size required by the software must be carefully considered before configuration, because the memory is not processed by the java garbage collection mechanism. Note that the maximum heap memory and the maximum non-heap memory cannot exceed the available memory of the operating system.


JVM optimization suggestions:
Ms/mx: defines the total size of the YOUNG + OLD segment, ms is the size of the YOUNG + OLD memory at JVM startup, and mx is the maximum size of the YOUNG + OLD memory that can be occupied. In the user production environment, these two values are generally set to the same, to reduce the overhead of the system in the memory application during running.
NewSize/MaxNewSize: defines the size of the YOUNG segment. NewSize is the memory size of YOUNG when JVM is started, and MaxNewSize is the maximum memory size of YOUNG. In the user production environment, these two values are generally set to the same, to reduce the overhead of the system in the memory application during running.
PermSize/MaxPermSize: defines the size of the Perm segment. PermSize indicates the memory size of the Perm when the JVM starts. MaxPermSize indicates the maximum available Perm memory size. In the user production environment, these two values are generally set to the same, to reduce the overhead of the system in the memory application during running.
SurvivorRatio: Set the ratio of VOR space to Eden space.

Possibility of memory overflow:

1. OLD segment Overflow
This memory overflow is one of the most common causes, which may be caused:
1) The set memory parameter is too small (MS/mx, NewSize/MaxNewSize)
2) program Problems

A single program continues to process memory consumption, such as string processing thousands of times. StringBuffer is recommended for string processing. At this time, no memory overflow error will be reported, but the system will continue to collect garbage and cannot process other requests. Related Problem programs can be obtained through Thread Dump (see system problem diagnosis chapter) the memory applied by a single program is too large. Some programs will apply for dozens or even hundreds of megabytes of memory. At this time, the JVM will also experience memory overflow due to the inability to apply for resources. First, find the relevant functions, then hand it to the programmer for modification. To find the relevant program, you must search for it in the Apache log.
After a Java object is used up, the referenced object is not destroyed, so that the JVM considers it to be an active object and does not recycle it. In this way, it occupies a large amount of memory and cannot be released. As there is no memory analysis tool on the market that has little impact on the system, it can only be located with programmers.
2. Perm segment Overflow
Usually, the Perm segment loads a large number of Servlet classes and causes overflow. The current solution is as follows:
1) Increase the PermSize. Generally, MB can meet the requirements.
2) If you have no choice, you can only add the servlet Path to the CLASSPATH. However, this is generally not recommended.

3. C Heap Overflow
The system has no restrictions on C Heap. Therefore, when a problem occurs in C Heap, the memory occupied by Java processes will continue to grow until all available system memory is occupied.
Others:
JVM has two GC threads. The first thread is responsible for revoking Heap's Young zone. When the Heap is insufficient, the second thread traverses the Heap and upgrades the Young area to the Older area. The size of the Older area is equal to-Xmx minus-Xmn, and the value of-Xms cannot be set too large, because the second thread is forced to run will reduce JVM performance.
Why does GC occur frequently in some programs? There are the following reasons:
L The program calls System. gc () or Runtime. gc ().
L some middleware software calls their own GC methods. In this case, you need to set parameters to disable these GC methods.
L Java Heap is too small. Generally, the default Heap value is very small.
L objects are frequently instantiated and Release objects. In this case, try to save and reuse objects, for example, using StringBuffer () and String ().
If you find that after each GC, the remaining Heap space will be 50% of the total space, which indicates that your Heap is healthy. Many Java programs on the Server end have a better free space of 65% after each GC.
Experience:
1. It is best to set-Xms and-Xmx to the same value on the Server JVM. To optimize GC, it is recommended that the-Xmn value be equal to 1/3 [2] of-Xmx.
2. It is best for a GUI program to run GC every 10 to 20 seconds and complete GC every time within half a second [2].
Note:
1. Increasing the Heap size reduces the GC frequency, but increases the GC time. When GC is run, all user threads are paused, that is, Java applications do not work during GC.
2. The Heap size does not determine the memory usage of the process. The memory usage of a process must be greater than the value defined by-Xmx, Because Java allocates memory for other tasks, such as the Stack of each thread.
2. Stack settings
Each thread has its own Stack.

 

-Xss Stack size of each thread

 

The Stack size limits the number of threads. If the Stack is too large, the memory may leak. -The Xss parameter determines the Stack size, for example,-Xss1024K. If the Stack is too small, the Stack may leak.
3. hardware environment
The hardware environment also affects GC efficiency, such as the machine type, memory, swap space, and number of CPUs.
If your program needs to frequently create many transient objects, this will lead to frequent jvm gc. In this case, you can increase the machine memory to reduce the use of Swap space [2].
4.4 GC Types
The first type is single-thread GC, which is also the default GC ., This GC is applicable to single-CPU machines.
The second type is Throughput GC, which is a multi-thread GC. It is suitable for programs with multiple CPUs and a large number of threads. The second type of GC is similar to the first type of GC. The difference is that the GC is multi-threaded in the Young area, but in the Old area and the first one, the single thread is still used. -XX: + UseParallelGC parameter to start the GC.
The third type is Concurrent Low Pause GC, which is similar to the first type. It is suitable for multiple CPUs and requires shorter time for program stagnation caused by GC. This GC can be used to run applications in the Old zone. -XX: + UseConcMarkSweepGC parameter to start the GC.
The fourth type is Incremental Low Pause GC, which is applicable to the requirement to shorten the time of program stagnation caused by GC. This GC can be used to reclaim some Old objects in the Young area. -The Xincgc parameter starts the GC.

 

 

 

Young generation:

All newly generated objects are first put in the young generation. The goal of the young generation is to quickly collect objects with short lifecycles as much as possible. The young generation is divided into three zones. One Eden zone and two vor zones (generally ). Most objects are generated in the Eden area. When the Eden zone is full, the surviving objects will be copied to the primary vor zone (one of the two). When the primary vor zone is full, the surviving objects in this region will be copied to another region vor. When the region vor region is full, the surviving objects will be copied from the first region vor region, it will be copied as "Tenured )". Note that the two regions of the same vor are symmetric and irrelevant. Therefore, objects copied from Eden and copied from the previous vor may exist in the same region, only objects copied from the first vor region are copied to the old district. In addition, there is always a blank vor area. At the same time, according to the program requirements, the VOR area can be configured as multiple (more than two), which can increase the time for the object to exist in the young generation and reduce the possibility of being put into the old generation.

 

Elder Generation:

Objects that are still alive after N garbage collection in the young generation will be put into the old generation. Therefore, it can be considered that objects with long lifecycles are stored in the old generation.

 

Permanent generation:

Used to store static files. Currently, Java classes and methods are supported. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some classes, such as Hibernate, in this case, you need to set up a large persistent storage space to store the classes added during the running process. The persistent generation size is set by-XX: MaxPermSize = <N>.

 

Under what circumstances will garbage collection be triggered?

Because the object is divided into generations, the garbage collection area and time are different. There are two types of GC:Scavenge GCAndFull GC.

 

Scavenge GC

Generally, when a new object is generated and the Eden application fails, Scavenge GC is triggered to perform GC on the Eden region to clear non-surviving objects, and move the surviving objects to the same vor area. Then, sort out the two zones in the same vor. In this way, GC is performed on the Eden area of the young generation and will not affect the old generation. Because most objects start from the Eden area and the Eden area is not allocated much, GC in the Eden area is performed frequently. Therefore, it is generally necessary to use fast and efficient algorithms so that Eden can be idle as soon as possible.

 

Full GC

Organize the entire heap, including Young, Tenured, and Perm. Full GC is slower than Scavenge GC because it is necessary to recycle the entire GC, so the number of Full GC should be minimized. In the process of JVM optimization, a major part of the work is to adjust FullGC. Full GC may occur due to the following reasons:

· The Tenured is full

· Perm is full

· System. gc () is displayed and called

· Dynamic Change of Heap allocation policies for various domains after the last GC

 

 

References: http://www.cnblogs.com/hnrainll/archive/2013/11/06/3410042.html

Related Article

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.