Introduction to JVM memory management and garbage collection mechanism

Source: Internet
Author: User

http://backend.blog.163.com/blog/static/20229412620128233285220/

Memory management and garbage collection are two of the core components of the JVM, and mastering its internal implementation is a prerequisite for Java developers to develop high-quality Java systems. Recently compiled some knowledge about JVM memory management and garbage collection, here to comb, share, and hope to have a deeper understanding of the Java Virtual machine.

1. JVM Memory Management

First, the JVM organizes memory into two parts of memory and working memory. Main memory includes the local method area and the heap. Each thread has a working memory, which consists mainly of two parts, one of which is the stack belonging to the thread and a register for the variable copy of the main memory part.

Figure 1 JVM Memory model

each JVM instance has a method area and a heap that is shared by all threads within that instance, and each new thread is started, and the JVM assigns the thread a working memory that belongs only to that thread. There are 3 main types of variables in Java, and the JVM's organization management of memory maps to the storage of variables, which can be understood as follows :

1) A static variable, shared by multiple objects in the same class, stored in the method area, shared by all threads;

2) instance variables, declared in the class, stored on the heap;

3) Local variables, declared within the method, stored on the stack;

Each thread can operate only its own working memory, and the thread cannot manipulate the main memory directly, unless the variable is modified with the volatile keyword, and the working memory cannot be directly accessed, and must pass through the master RAM.

2. Garbage collection mechanism

Java garbage Collection is fully responsible for the JVM and is basically transparent to the user. The basic reason is that:

1) The user can use the Finalize function to dispose of the memory space requested by the local method, or to close the file, before the garbage object is reclaimed.

2) The user can use System.GC () to recommend that the JVM be garbage collected, but it should be noted that this is only a recommendation, as to when to recycle, whether the recovery is unknown, is fully responsible for the JVM.

The JVM detects heap space with independent, low-priority threads. The whole garbage collection mechanism involves two fundamental issues: one is to determine what is rubbish. The second is to release the memory space occupied by the garbage object.

First, let's take a look at the first question, where objects in Java that are not referenced by other objects are garbage objects, and there are four types of object references in Java: Strongreference, SoftReference, WeakReference, Phantomreference.

1) strongreference: Java Generic object reference.

2) SoftReference: If an object is SoftReference referenced, and no other strongreference points to it, the object can be garbage collected at any time, But this time generally occurs when other threads are suspended because of insufficient memory space before the outofmemory error occurs. SoftReference is typically used for references to objects within the cache.

3) WeakReference: If an object is WeakReference referenced, and no other strongreference and softreference references point to him, the object is immediately recycled. WeakReference is an eager garbage collection relative to the Softreferen CE.

4)Phantomreference: If an object is phantomreference referenced, and no other strongreference, SoftReference and The WeakReference reference points to it, and the object can be garbage collected at any time. phantomreference references are typically used to detect objects entering Referencequeue, and for objects that do not implement the Finalize method to release the memory space requested by the local method and to close the file before garbage collection.

after knowing what is rubbish, I would like to introduce the garbage collection algorithm of the JVM. At present, themainstream garbage collection algorithm of JVM is a kind of algorithm called generational garbage collection. I think that the use of this algorithm is a foothold: inJAVA , most of the objects are short-term, can be a long time existence of the objects accounted for a few. It is based on the fact that I think it is necessary to the Java main memory space according to the object's survival period to divide.

The sub-generational garbage collection algorithm mainly includes the following three recovery algorithms: Copy collection,mark-sweep,mark-compacting.

1) Copy collection: Copies the objects in one area to another free zone, erasing all the contents of the original region. The advantage of copy collection is that it has a higher benefit, and the disadvantage is that it requires an extra memory space. So it is suitable for small garbage collection. This algorithm is commonly used by MAJORGC in the JVM.

2) Mark-sweep: Called Mark Sweep collection, first scans all references in the zone, marks unreachable objects, and then erases the tagged objects. The advantage of Mark-sweep is that there is no need to move elements to enable garbage collection, and there is no need for additional free memory areas. The disadvantage is that it generates a lot of memory fragmentation. At the same time mark-sweep memory allocation is a kind of freelist way, is to make all the space area a List, each allocation of space is from freelist to find a satisfying condition of the region allocation. This method of distribution is inefficient and complex.

3) mark-compacting is called the tag compression collection, first scans the entire space, marks the objects and unreachable objects, and then erases the unreachable objects and moves the objects into a contiguous space. The advantage of this algorithm is that it does not produce memory fragmentation. Both it and the memory allocation collected by replication use a method called a free pointer, which points to the space of the last object, each time it is allocated directly from the back space. This memory allocation method is highly efficient. The disadvantage of mark-compacting is the need to move elements. Also, in addition to marking unreachable objects, mark the objects that can be reached. less efficient. Mark-sweep and mark-compacting are mainly used for the recovery of old generation memory.

The main memory space of Java is divided into 3 parts: The New Generation (young), the old generation (tenured) and the Perm area. The Cenozoic is divided into 3 parts : Eden and two survivor areas. All objects created with new are placed in the Eden area, and thesurvivor area is primarily used to replicate the collected target space. So two survivor always have at least one that is free.

Figure 2 JVM Generation Division

The new generation of garbage collection algorithm is the replication collected, in my opinion, the reason is that replication collection efficiency is high. But additional memory space is required, and this memory space is survivor. The new generation of garbage collection, called MANORGC,JVM, provides three new generation of garbage collector:

1) Serial GC: Complete the entire replication collection process with one thread, requiring the application to pause execution. In the case of single CPU, the efficiency is higher than the other two kinds. However, it causes the application to have a long pause.

2) Parallel GC: Use multithreading to complete the entire replication collection process, reducing the time of application pauses.

3) Parnew: The same as the parallel GC, the only difference is that the design parnew is mainly used in conjunction with the old generation of CMS concurrent collection use. In the old generation of CMS process, it is possible to start a new generation MANORGC, change the reference relationship of the object, resulting in the old generation of errors, parnew is specifically to deal with the situation. Special processing is added to ensure the correctness of the object relationship. So the old generation of CMS must match the new generation of parnew together.

The old generation garbage collection algorithm mainly uses Mark-sweep and MARK-COMPACTING,JDK to provide three kinds of old generation of garbage collector:

1) Serial GC: The collector uses the mark-compacting algorithm to complete the tagging and compression process with a single thread, which takes a long time to pause the execution of the application, and in the case of a single CPU, it is more efficient than the other two.

2) Parallel GC: The parallel mark-compacting algorithm used by the collector, firstly, using multithreading to mark objects and unreachable objects, using a thread to determine the target region and source region of the movement, and moving the compression with multi-threaded concurrency. The advantage is concurrent execution, which can reduce application pause time, the disadvantage is moving elements, inefficient.

3) CMS GC: Concurrent GC, using the MARK-SWEEP algorithm, first using a thread to mark directly accessible objects. Then use multithreading to mark objects that can be indirectly reached. Because the second stage is a concurrency token, it is possible that the application modifies the reference relationship of the object during the tagging process, so the third step is to use a single thread to mark the modified reference relationship. Finally, it is cleared with multithreading. The advantage of this algorithm is that most of them are concurrent processes, so it can greatly reduce the pause time of the application, but the disadvantage is that the 3 marks, the total time is longer than the parallel GC.

Introduction to JVM memory management and garbage collection mechanism

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.