In-depth analysis of Java garbage collection mechanism

Source: Internet
Author: User
Tags java se

  • Introducing Garbage Collection
  • What memory needs to be recycled?
  • Reference counting method
  • Analysis of accessibility
  • How to Recycle
  • Marking Mark
  • Normal deletion Clear
  • Deletion with compacting compression
  • Why do I need generational collection?
  • Generational generation of the JVM
  • Cenozoic
  • Old age
  • Permanent generation
  • Details of the generational garbage collection process
Introducing Garbage Collection

Program counter, virtual machine stack, local method stack 3 regions with thread, out of the thread, stack frame in the stack with the method to enter and exit the orderly execution of the stack and into the stack operation. How much memory is allocated in each stack frame is basically known when the class structure is determined (although it is run by the JIT compiler
Some optimizations, but in this chapter based on the conceptual model of the discussion, generally can be considered as the compile time known, so the memory allocation and recovery of these areas are deterministic, in these areas there is no need to consider the recycling of the problem, because the end of the method or the end of the thread, the memory naturally with the recycling. While the Java heap and the method area are different, multiple implementation classes in an interface may require different memory, and multiple branches in one method may require different memory, we can only know when the program is running, which objects will be created, this part of the memory allocation and recycling is dynamic, The garbage collector is concerned with this part of the memory-----"in-depth understanding of Java virtual machines"

The automatic garbage collection mechanism is to look for objects in the Java heap, classify objects, look for objects that are being used and objects that are not already in use, and then purge objects that are not used from the heap.
The automatic garbage collection mechanism is to solve three problems:

    • What memory needs to be recycled?
    • When do I recycle?
    • How to recycle?
What memory needs to be recycled? Reference counting method

The simplest way to determine whether or not to use the first question is to see if there is a reference to the object now, and if it does not indicate that the object is no longer being used, if there is a reference to whether the object may continue to be used, this method of referencing existence is called the reference counting method. However, one problem with this method is that it cannot solve the problem of object circular reference, so there is a method of accessibility analysis to determine whether an object can be recycled.

Analysis of accessibility

The basic idea of this algorithm is to use a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, the path of the search is called the reference chain (Reference Chain), when an object to the GC Roots no reference chain connected (in the case of graph theory, Is that this object is not available when the GC roots to this object is unreachable).
In the Java language, the objects that can be used as GC roots include the following:

    • The object referenced in the virtual machine stack (the local variable table in the stack frame).
    • The object referenced by the class static property in the method area.
    • The object referenced by the constant in the method area.
    • The object referenced by JNI (that is, generally speaking, the native method) in the local method stack.
How to Recycle

Garbage collectors often help us to automatically recycle in the background. For the specific recycling process as long as you have the following steps

    • Step 1:marking Marking

The first step is tagging, which means that the garbage collector will find the memory of the objects that need to be recycled and the memory of objects that do not need to be recycled, and mark them out, simply by finding out where the rubbish is.

All objects in the heap are scanned once to determine which objects are recycled, so this is usually a relatively time-consuming process

    • Step 2:normal Deletion
      The garbage collector clears the area of objects that need to be recycled from the previous step.

The problem is fragmentation:
After the mark is cleared, there is a lot of discontinuous memory fragmentation, and too much space fragmentation can cause future
When a large object needs to be allocated during the sequence run, it cannot find enough contiguous memory and has to trigger another garbage collection action in advance.

    • Step 2a:deletion with compacting compression
      Because the simple cleanup can be fragmented, there is a way to compress the purge, that is, to clear the objects that need to be recycled, and then compress the memory, the memory is divided into usable and unavailable two parts

Why do I need generational collection?

As mentioned earlier, the process of tagging objects and compressing memory is not efficient in the JVM, and the more objects are allocated, the longer the garbage collection takes. However, some empirical statistical analysis shows that most of the objects in a program are short-lived!

is a similar statistic, the ordinate indicates the amount of memory occupied by the allocated object, and the horizontal axis indicates the time since the object was allocated.

We see that most of the objects die long before they live, but only a few of them live longer.

Generational generation of the JVM

In order to increase the efficiency of garbage collection, the JVM divides the heap into different parts, usually three parts, the new generation, the old age and the permanent generation

Cenozoic

All newly created objects will appear first in the Cenozoic, and when the new generation of this part of the memory is full, a garbage collection event is initiated, which occurs in the next generation of garbage collection called minor collections. This collection is usually faster because most of the new generation's objects need to be recycled, and those that are temporarily not recoverable will be moved to the old age.

Stop The World event-all minor garbage collections is the stop the world event, meaning that all application threads need to be stopped until the garbage collection operation is complete. Similar to
"When your mother is cleaning your room, it will certainly let you stay in the chair or outside the room, if she cleans, you throw scraps of paper, this room can be cleaned?" ”

Old age

The old age was used to store objects that had long survived. In general, we will limit the new generation of objects to a surviving time, and when this time has not been collected, it will be moved to the old age. Garbage collection in the old age area is called major garbage collection

Major garbage Collection is also a Stop the World event. Usually the major garbage collection are relatively slow, as the collection of the old age includes the collection of all objects, that is, the collection of objects of the new generation and the old ages.

Permanent generation

The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application . The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE Library classes and methods is stored here.

Classes may get collected (unloaded) if the JVM finds they is no longer needed and space may is needed for other Classes. The permanent generation is included in a full garbage collection.

Details of the generational garbage collection process

We already know the methods required for garbage collection and the generation of heap memory, so let's take a look at the specific process of garbage collection.

    • In the first step, all new objects are first assigned to the Cenozoic region, and two survivor regions are initialized to be empty.

    • In the second step, when the Eden area is full, a minor garbage is raised collection

    • The third step, when the minor garbage collection, the surviving objects will be moved to the S0survivor area

    • Fourth step, and then when the Eden area fills up again, the next garbage collection will occur, and the surviving objects will be moved to the survivor area and the surviving objects will be deleted directly. However, the difference is that in this garbage collection, both the surviving objects and the objects in the previous survivor are moved to the S1. Once all the objects are moved to S1, the objects in the S2 are cleared, the objects in the graph are carefully observed, and the numbers indicate the number of garbage collections that have gone through. Currently we have different age targets.

    • In the fifth step, the next garbage collection, the last step is repeated, the objects that need to be reclaimed are cleared, and the survivor area is switched again, and all surviving objects are moved to S0. The Eden and S1 areas are cleared.

    • The sixth step, repeat the above steps, and record the age of the object, when the age of the object reached a certain threshold, the new generation of objects moved to the old age. In this case, the threshold value is 8.

    • The seventh step, the garbage collector will repeat the above steps, the elimination of the object and the age of the movement

    • Finally, we observed that the majority of the garbage collection process is carried out in the next generation, until the old age of memory is not enough to launch a major GC, will be labeled and organized compression.

In-depth analysis of Java 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.