Java GC mechanism and related

Source: Internet
Author: User

Record learning on the road to see, if there are flaws and hope to forgive. Alice

Garbage (garbage) is the object that the program needs to recycle, if an object is not directly or indirectly referenced, then this object becomes "garbage", it takes up the memory needs to be released in time, otherwise it will cause "memory leak". Some languages require programmers to manually free up memory (garbage collection), some languages have garbage collection mechanisms (GC), such as the Java language I am learning, and there is a garbage collection mechanism.

Before we learn the Java GC, we need to remember one word: Stop-the-world. It will occur in any GC algorithm. Stop-the-world means that the JVM stops executing the application because it needs to perform a GC. When stop-the-world occurs, all threads go into a wait state, except for the thread required by the GC, until the GC task completes. GC optimization is a lot of the time to reduce the occurrence of Stop-the-world, shorten the time due to GC thread interruption, to ensure the efficiency of program operation.

The first type of marker clears

Mark Cleanup (Mark and Sweep) is the first GC algorithm developed.

Principle: Two scans, the first time from the root of the potentially referenced object will be tagged in a recursive way, the second time the Unlabeled object is reclaimed, after the successful recycle, the marked Reference object is cleared to be marked for the next GC recovery preparation.

GC Time: Related to the number of surviving objects and the total number of program objects, because recursive viewing of all objects can be recycled.

Think: If the number of surviving objects is small, then the actual tag stage is very few, the rest is not the reference object, then the efficiency of the marking phase will be reduced, thus the efficiency of the whole GC process will be reduced.

Mark Erase saves memory but two scans require more time, which is advantageous for smaller garbage ratios.

The second type of replication collection

Copy collection (copy and Collection) copies the object referenced from the root to another space, then discards the old space and takes advantage of the new space.

Principle: The method of copy collection requires only one scan of the object. Prepare a "new space", start with the root, sweep the object, and if there is a reference to the object, copy it to "New space". After a scan is complete, all objects that exist in the "new space" are all non-garbage objects.

Thinking: Replication collection is local, and in the process of copy collection, objects are copied into the new space in the order in which they are referenced. As a result, the likelihood of a closer object being placed in a more distant memory space is increased, which is called locality. In the case of high locality, the memory cache will work more efficiently and the performance of the program will improve.

Replication collection is faster but requires an extra piece of memory to replicate, which is advantageous for large garbage ratios.

The third type of reference count

Reference counting means that, for each object, a reference count for that object is saved, and the reference count for that object increases. If the object's reference count is zero, the object is recycled.

Principle: Each object holds a reference count for the object, and the count is updated when the reference increases or decreases, and the time point occurs: Variable assignment, object content Update, function end (local variable is no longer referenced). When an object has a reference count of zero, it means that it is no longer referenced and frees the appropriate memory space.

  

Thinking: The greatest advantage of reference counting is that it is easy to implement. The cost is small, basically the reference count is 0 when the garbage is immediately recycled, and other methods difficult to predict the life cycle of the object, the garbage will exist longer than this method. This garbage collection method produces the shortest interruption time.

However, if there is a circular reference in the object, it cannot be reclaimed. Reference counts do not fit in parallel, and multiple threads manipulate reference counts at the same time, causing problems with different values causing memory errors. So the reference count must be exclusive, and if the reference operation is frequent, then the overhead of concurrency control mechanisms such as locking is quite large.

Java GC (garbage collection Mechanism)

The JVM GC reclaims only the objects in the heap area and the method area . The data in the stack area is automatically freed by the JVM when it goes out of scope, so it is not within the scope of the JVM GC's management.

Criteria for determining whether an object can be recycled:

  1. Object does not have a reference
  2. No catch exception occurred at scope
  3. The program executes properly at scope
  4. The program executed the System.exit ()
  5. Unexpected termination of program (killed thread, etc.)

There is no way to explicitly allocate and unregister the cache in a Java program, because these things the JVM has done for us, that is, the GC.

Sometimes we can set the related object to null to try to display the clear cache, but not set to NULL will be marked as recyclable, it is possible to escape.

Setting the object to null is at least harmless, but using System.GC () is not available, and using System.GC () is not a GC operation immediately, but it waits for a period of time, not even execution, and System.GC (), if executed, triggers the full GC, which greatly affects performance.

Java garbage collection is generational recovery, divided into three generations: the new generation, the old age, lasting generations.

New Generation (young generation): The vast majority of newly created objects are assigned here, and since most of them become inaccessible soon after they are created, many objects are created in the Cenozoic and then "disappear." The process by which an object "disappears" from this area is called: Minor GC.

Old generation: objects have not become unreachable, and survived from the Cenozoic cycle, in the new generation of the battlefield bravely survived the object, is not easy to take the dog, hehe. will be copied here. Its region allocates more space than the Cenozoic. Because of its relatively large space, the number of GCs occurring in the old years is much less than in the Cenozoic. The process of disappearing an object from the old age, which is called: Major GC or full GC.

The persistent generation (Permanent generation) is also known as the method area: used to hold class constants and string constants. Note that this area is not used to store objects that survived from the old age, and this area can also occur in GC. GC events that occur in this region are also counted as Major GC. Only the conditions in which GC is occurring in this area are very stringent and must meet the following three conditions to be recycled:

1. All instances are recycled

2. ClassLoader to load the class is recycled

3. Class objects cannot be accessed by any means (including reflection)

Structure of the Cenozoic and the old age: the formation and logic of Cenozoic space

To better understand the GC, let's learn the composition of the new generation, which is used to preserve the objects that were created for the first time, and it is divided into three spaces:

· An Eden Space (Eden)

· Two survivor space (Fron Survivor, to Survivor)

Allocation of default Cenozoic space: Eden:Fron:To = 8:1: 1

The order of execution for each space is as follows:

1. Most of the objects that have just been created will be stored in the Eden Space (Eden).

2. After the first GC (Minor GC) is executed in Eden space, the surviving object is moved to one of the survivor spaces (Survivor).

3. Thereafter, each time a GC is performed in Eden Space, the surviving objects are stacked in the same survivor space .

4. When a survivor is saturated with space, the surviving object is moved to another survivor's space. It then empties which survivor space is already saturated.

5, repeat in the above steps n times (n = maxtenuringthreshold (age threshold setting, default 15)) still live objects, will be moved to the old age.

From the above steps can be found that two survivor space, must have one is kept empty . If two two survivor spaces have data, or two spaces are empty, there must be some kind of error in your system.

The important point to remember is that after the object has just been created, it is stored in the Eden space. Those long-lived objects are transferred to the old generation through Survivor Space (Survivor).

There are exceptions, for some larger objects (which need to allocate a larger contiguous memory space) go straight to the old age. Generally occurs in the case of survivor space shortage.

the composition and logic of the old age space

The composition of the old age space is actually very simple, it does not like the new generation of space as divided into several areas, it has only one region, the objects stored inside is not like the vast majority of the new generation of space is justified man road, Xi died. almost all of the objects here are from Survivor in the space, they will never be easy dog belt . As a result, the full GC (Major GC) does not occur as frequently as minor GC, and the time to do Major GC is longer (about 10 times times) than minor GC.

2018.01.30 Record

Java GC mechanism and 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.