GC (Garbage collection)

Source: Internet
Author: User

Java--gc

tags (space delimited): Java

To get an insight into the Java GC (garbage Collection), we should first explore the following three questions:

    • What? -What memory needs to be recycled?
    • When? -When is it recycled?
    • How? --How to recycle?
GC Definition

Definition:program itself finds and collects memory which is useless. It is a form of automatic memory management which doesn ' t need programmers release memory.
Why is there a GC mechanism in Java?

    • security considerations;--for security.
    • Reduced memory leaks--erase memory leak in some degree.
    • Reduce programmer workload. --Programmers don ' t worry about memory releasing.
What? -What memory needs to be recycled?

We know that the memory runtime JVM will have a runtime data area to manage memory. It consists of 5 major parts: program Counter Register, virtual machine stack (VM stack), local method Stack (Native), method area, heap .

The program counter, the virtual machine stack, and the local method stack are the private memory spaces of each thread, which are born with threads and die with threads. For example, how much memory is allocated in each stack frame in the stack is basically known when the class structure goes down, so the memory allocation and recycling of these 3 regions is deterministic, regardless of the memory reclamation issue.

However, the method area and the heap are different, the memory of an interface may require more than one implementation class, we only know what objects will be created while the program is running, this part of the memory allocation and recycling is dynamic, the GC is mainly concerned about this part of memory.

In summary, the GC's primary recovery memory is the method area and heap in the JVM;
It involves a multi-threaded (referred to heap), multiple reference to different types of the object (referred to as a method area), before the GC is involved in recycling.

When? -When is it recycled? Heap

In the interview often encounter such a problem (in fact, I have encountered): How to judge that an object has died?

An easy-to-think answer is to add a reference counter to an object. The counter value is incremented by 1 whenever there is a place to refer to it, and the counter value is reduced by 1 when the reference fails. And when the value of the counter is 0 o'clock the object is no longer used and is judged to be dead. is not simple and intuitive. However, it is regrettable. This practice is wrong ! (You don't have to answer that when you're interviewing.) Why is it wrong? In fact, using the reference counting method is a good solution in most cases, but there are many cases in the actual application, but it can't solve the circular reference problem between objects. For example, there is a field in object A that points to object B, and Object B has a field pointing to object A, and in fact they are no longer used, but the value of the counter will never be 0, it will not be recycled, and then a memory leak occurs.

So what should be the right thing to do?
In java,c# and other languages, the main way to determine the death of an object is: accessibility analysis (reachability).
All generated objects are a subtree called the root of the GC Roots. Starting from GC roots, the search path is referred to as the reference chain (Reference Chain), when an object to the GC roots no reference chain can be reached, it is called the object is unreachable (not referenced), that is, can be recycled by GC. As shown in the following:

[Accessibility algorithm determines whether objects can be recycled] [1]

Whether it's a reference counter or a accessibility analysis, determining whether an object survives is a reference! So, how do you define a reference to an object?

We would like to give a description of how the memory space can be stored in memory, and if the memory space is still very tight after garbage collection, discard the objects. Therefore, according to different requirements, the following four kinds of references, depending on the reference type, GC recycling will also have different operations:

    • Strong reference (Strong Reference): Object obj = new Object (); The GC never reclaims the referenced object as long as a strong reference exists.
    • Soft references (Soft Reference): Describes some objects that are also useful but not necessary. Before the system will have a memory overflow, these objects will be included in the recycling scope for two recycling (that is, the system will have a memory overflow before they are recycled.) )
    • Weak references (Weak Reference): Somewhat weaker than soft references. These objects can only survive until the next GC. When the GC is working, it is recycled whether or not the memory is sufficient (that is, they are recycled whenever a GC is performed). )
    • Virtual reference (Phantom Reference): Whether an object has a virtual reference does not have an effect on its time-to-live.
Method area

What we have already mentioned is that the GC primarily reclaims the memory in the heap and the method area , and how the above is primarily for object recycling, they are generally located in the heap. So how do you recycle things in the method area ?

There are some obsolete constants and useless classes that need to be recycled in the method area.

    1. The recycling of obsolete constants. This is where the reference count can be seen. No object references the constant and can be safely recycled.
    2. Recycling of useless classes. What is a useless class?
    • All instances of this class have been recycled. That is, no instance of the class exists in the Java heap;
    • The ClassLoader that loaded the class have been recycled;
    • The corresponding Java.lang.Class object of this class is not referenced anywhere, and cannot be accessed anywhere by reflection on the method of the class.

In summary, for objects in a heap , the main use of accessibility analysis is to determine whether an object still has a reference, if the object does not have any references should be recycled. And according to the different requirements of our actual reference, it is divided into 4 references, each kind of reference recycling mechanism is also different.
For constants and classes in the method area , when a constant does not have any objects referencing it, it can be recycled. For a class, if it can be judged to be a useless class, it can be recycled.

How? --How to recycle? Tag-Purge (Mark-sweep) algorithm

It is divided into two stages: first, all objects that need to be recycled are marked, and all tagged objects are collected uniformly after the mark is complete.
Cons: Efficiency issues, marking and clearing two processes are inefficient; Space problems can create a lot of fragmentation.

Replication Algorithms

Divide available memory by capacity into two blocks of equal size, with only one piece at a time. When this piece is run out, copy the surviving object to the other piece, and then reclaim all the original space. Efficient and simple.
Cons: Reduce the memory to half the original.

Mark-and-organize (MARK-COMPAT) algorithm

The tagging process is the same as the mark-and-clear algorithm, but instead of a simple purge, all surviving objects are moved to one end and the memory outside the end boundary is cleared directly.

Generational collection (generational Collection) algorithm
    • In the Cenozoic, every garbage collection has a large number of objects die, only a small number of survival, the choice of replication algorithm, only need to pay a small number of living objects copy cost can be completed collection;
    • In the old age, where the survival rate is high and there is no additional space to guarantee it, it should be recycled using the "mark-and-sweep" or "mark-sweep" algorithm.
Some Collectors serial collectors

A single-threaded collector, which indicates that all other worker threads must be paused until it is collected at the end of the garbage collection. "Stop the World".

Parnew Collector

is actually a multithreaded version of the serial collector.

    • Concurrency (Parallel): Refers to multiple garbage collection threads working in parallel, but at this point the user thread is still in the waiting state;
    • Parallel (Concurrent): The user thread executes concurrently with the garbage collection thread, the user program continues to run, and the garbage collection program runs on the other CPU.
Parallel Scavenge Collector

The collector pays attention to throughput (throughout) (the ratio of the CPU's time to the total CPU time spent on the user's code) and ensures that the throughput is within a manageable range.

CMS (Concurrent Mark Sweep) collector

The CMS collector is a collector with the goal of getting the shortest pause time.

G1 (garbage first) collector

Since JDK1.7 Update 14, the hotspot virtual machine officially provides a commercially available G1 collector, which has the following advantages over other collectors: parallel and concurrency, generational collection, spatial integration, predictable pauses, etc.

This section mainly analyzes three different garbage collection algorithms: Mark-sweep, Copy, Mark-compact. Each algorithm has different advantages and disadvantages, and also has a different scope of application. There is no strict requirement for the garbage collector in the JVM, and different collectors combine multiple algorithms for garbage collection.

Memory allocation

The automatic memory management advocated in the Java technology system can ultimately be attributed to automating the resolution of 2 problems: allocating memory to Objects and reclaiming memory allocated to Objects .

Object Precedence in Eden Assignment

In most cases, objects are allocated in the Cenozoic Eden region. When there is not enough memory in the Eden area, the virtual machine will initiate a minor GC.

    • Minor GC (Cenozoic GC): Refers to the garbage collection action that occurs in the Cenozoic, because most Java objects have an out-of-date feature, so Minor GC occurs very often.
    • Full Gc/major GC (old age GC): Refers to GC that occurs in the old age, Major GC appears, often accompanied at least once minor GC.
Large objects direct into the old age

Large objects are Java objects (such as long strings and arrays) that require a large amount of contiguous memory space.

Long-lived objects will enter the old age.

The JVM defines an object age counter for each object.

    • If the object survives after Eden was born and has experienced the first minor GC and can be accommodated by survivor, it should be moved to survivor space, and the age object set to 1;
    • Object in the Survivor area each time minor GC, age will increase by 1 years, when its age increased to a certain extent (by default, 15 years old, can be set by the parameter-xx:maxtenuringthreshold), will be promoted to the old age.
    • It is important to note that the JVM does not always require that the age of the object must reach Maxtenuringthreshold in order to be promoted to the old age, if the sum of all objects of the same age in Survivor space is greater than the general size of survivor space, Objects older than or equal to that age can go straight into the old age, without having to wait for the ages required in the maxtenuringthreshold.
Space Allocation guarantee
    • Before the minor GC occurs, the virtual opportunity first checks whether the largest available contiguous space in the old age is greater than the total space of all new generation objects, and if this condition is true, the minor GC is safe;
    • If not, the virtual opportunity to see if the Handlepromotionfailure setting value allows the warranty to fail. If allowed, then hurry to check whether the largest available continuous space in the old age is greater than the average size of the previous promotion to the old age object, if greater than, will try to do a minor GC, although it is risky;
    • If less than or handepromotionfailure is set to not allow adventure, then a full GC will be performed instead.
Summarize

This blog mainly based on the GC principle of Java, from the what,when,how three aspects of how to carry out garbage collection analysis.
Nutshell:
What--heap and method area;
When-dead objects (references cannot be reached);
How--mark-Erase-organize-copy algorithm.
On the GC problem, firmly grasp the three problems, and then the divergent thinking, you can well grasp this part of the content.
Finally, the paper introduces the memory allocation policy of Java to the object: The New generation Eden region--Survivor district--the old age

Reprinted from Http://www.cnblogs.com/little-YTMM/p/5613642.html

GC (Garbage collection)

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.