In-depth understanding of Java Virtual Machine learning note 3--garbage collection algorithm

Source: Internet
Author: User

In the memory area of the Java Virtual machine, the program counter, the virtual machine stack, and the local method stack three areas are thread-private, generates with the thread, and out of line, with the stack frame in the stack and out of the stack as the method enters and exits, the amount of memory allocated per stack frame is basically known when the class structure is determined. As a result, memory allocation and recycling in these three regions are deterministic. Garbage collection focuses on the memory of the heap and the section of the method area.

The commonly used garbage collection algorithms are:

(1). Reference Counting algorithm:

Add a reference counter to the object, and whenever there is a place to reference it, the counter value is incremented by 1, and when the reference fails, the counter value is reduced by 1, and any object with a counter of 0 is no longer used, and the garbage collector reclaims the memory used by the object.

The reference counting algorithm is simple and efficient, Microsoft's COM technology, ActionScript, Python and so on have used the reference counting algorithm for memory management, but the reference counting algorithm is difficult to solve the mutual circular reference problem between objects, so Java does not use the reference counting algorithm.

(2). Root Search algorithm:

Through a series of objects named "GC Root" as a starting point, searching down from these nodes, the path of the search is called the reference chain (Reference Chain), when an object to GC root does not have any reference chain connected, the object is unreachable, the object is not available, The garbage collector reclaims the memory it occupies.

Mainstream business programming Languages C #, Java, and Lisp all use the root-search algorithm for memory management.

In the Java language, objects that are available as GC root include the following objects:

A. The referenced object in the Java Virtual machine stack (the local variable table in the stack frame).

B. The object referenced by the class static property in the method area.

C. The object referenced by a constant in the method area.

D. The reference object for the Jni local method in the local method stack.

Java method Areas are called permanent generations in the Sun hotspot virtual machine, many people think that the part of the memory is not recycled, the Java Virtual Machine specification does not have a garbage collection of this part of the memory provisions, but the method area of obsolete constants and useless classes still need to be recycled to ensure that the permanent generation will not occur memory overflow.

Method of judging obsolete constants: If a constant in a constant pool is not referenced by any reference, the constant is an obsolete constant.

To judge a useless class:

(1). All instances of the class have been reclaimed, that is, the instance object of the class does not exist in the Java heap.

(2). The class loader that loaded the class has been recycled.

(3). The Java.lang.Class object of the class is not referenced anywhere, and the method of the class cannot be accessed anywhere through the reflection mechanism.

Common garbage collection Algorithms in Java:

(1). Mark-Clear algorithm:

The most basic garbage collection algorithm, the algorithm is divided into "mark" and "clear" two stages: first mark out all the objects that need to be recycled, after the mark is complete, the unified collection of all tagged objects.

There are two drawbacks to the mark-and-purge algorithm: First, efficiency issues, marking and clearing efficiency are not high. Second, after the tag is cleared, there is a lot of discontinuous memory fragmentation, and too much space fragmentation can cause the program to be unable to find enough contiguous memory when it needs to allocate memory for a large object and has to trigger another garbage collection action in advance.

(2). Copy algorithm:

The available memory is divided into two blocks of equal size, one at a time, when the memory is used, the surviving objects are copied to another memory, and the used memory space is cleaned out once. This makes each time a piece of memory to be recycled, memory allocation without regard to memory fragmentation and other complex situations, only need to move the heap top pointer, in order to allocate memory, easy to implement, efficient operation.

The disadvantage of the replication algorithm is obvious, and the memory that can be used is reduced to half the original.

(3). Tag-Collation algorithm:

The tagging-finishing algorithm has been improved on the basis of the mark-and-sweep algorithm, which marks all the objects that need to be recycled, and does not clean up the recyclable objects directly after the tag is complete, but instead allows all surviving objects to move toward one end, clearing recyclable objects during the move, a process called grooming.

The advantage of the mark-and-sweep algorithm compared to the tag-purge algorithm is that memory fragmentation does not result in a large number of discontinuous memory fragments.

In the case of high object survival rate, the replication algorithm will perform more replication operations, and the efficiency can be reduced, and the efficiency of the marker-collation algorithm will be greatly improved with the high object survival rate.

(4). Generation of collection algorithms:

Depending on the lifetime of the objects in memory, dividing the memory into chunks, Java's virtual machines generally divide memory into cenozoic and older generations, and when new objects are created, they typically allocate memory space in the Cenozoic, and when the next generation garbage collector recovers several times, the surviving objects are moved to the old generation memory. When large objects cannot find enough contiguous memory in the Cenozoic, they are created directly in the old generation.

Now the Java Virtual Machine is federated with the generational replication, tag-purge and tag-grooming algorithms, and the Java Virtual machine garbage collector's memory structure is as follows:

Heap memory is divided into two parts of the Cenozoic and older generations, and the entire heap memory uses a generational replication garbage collection algorithm.

(1). New Generation:

New generation using replication and labeling-clearing garbage collection algorithms, the study shows that 98% of the new generation of objects is facing the short life cycle of the object, so do not need to divide the Cenozoic into two parts of equal size memory, but will be divided into Eden region, Survivor from and Survivor To three parts, which account for the Cenozoic memory capacity of the default ratio of 8:1:1, where survivor from and survivor to always have a region is blank, only Eden and one survivor a total of 90% of the Cenozoic capacity for the newly created objects to allocate memory, Only 10% of survivor memory is wasted, and when the new generation of memory is not enough to be garbage collected, the surviving objects are copied into the empty Survivor memory Area, Eden and non-blank survivor are marked-cleaned and reclaimed, and two survivor regions are rotated.

In the Cenozoic 98%, the blank survivor can store objects that are still alive at garbage collection, 2% in extreme cases, if the empty survivor space cannot hold the surviving objects, the memory allocation guarantee mechanism is used to directly copy the new generation of surviving objects to the old generation memory. Also, when creating large objects, if there is not enough contiguous memory in the Cenozoic, the memory space is allocated directly in the old generation.

Java Virtual machines for the new generation of garbage collection called minor GC, the number of times more frequent, each recovery time is also relatively short.

Use the Java Virtual Machine-xmn parameter to specify the Cenozoic memory size.

(2). Old generation:

In older generations, objects are generally long life-cycle objects, and the survival rate of the objects is high, so the use of tag-sorting garbage collection algorithms in older generations.

Java virtual machines are known as Majorgc/full GC for older generations of garbage collection, with relatively few times and a longer time-to-recovery.

When there is not enough space in the Cenozoic to create allocated memory for an object, memory reclamation in older generations cannot reclaim enough memory space, and the heap generates OUTOFMEMORYERROR exceptions when the new generation and old generation space cannot be expanded.

The Java Virtual Machine-XMS parameter can specify a minimum memory size, and the-XMX parameter can specify the maximum memory size, which subtracts the Cenozoic memory size specified by the XMN parameter, and calculates the minimum and maximum memory capacity of the old generation.

(3). Permanent Generation:

The method area in the Java Virtual Machine memory, known as the permanent generation in the Sun hotspot virtual machine, is a memory area shared by each thread that stores data such as class information, constants, static variables, and immediately compiled code, which have been loaded by the virtual machine. Permanent garbage collection is less efficient, but it must also be garbage collected, otherwise it will still throw a OutOfMemoryError exception if the memory is not sufficient for permanent generation.

The permanent generation also uses the tag-collation algorithm for garbage collection, and the Java Virtual machine parameters-xx:permsize and-xx:maxpermsize can set the initial size and maximum capacity of the permanent generation.

(EXT) "In-depth understanding of Java Virtual Machine" Learning note 3--garbage collection algorithm

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.