GC portion of Java notes

Source: Internet
Author: User
Tags compact

Introduction: Garbage collection mechanism, we all know that Java garbage collection is automatically reclaimed by the JVM, do not need the programmer to manage. But we still have to know the principle to tune the JVM at the right time.

Principle: When we new an object, the JVM heap allocates a chunk of memory (address, size) to this object, and when the object is "unreachable" (that is, when the program is unreachable), the GC needs to reclaim the space.

The Java language Specification does not explicitly describe which garbage collection algorithm the JVM uses, but any garbage collection algorithm typically does 2 basic things: (1) Discovers useless information objects, (2) reclaims the memory space occupied by the useless objects so that the space can be reused by the program.

Algorithms include:

  1. Reference count (Reference counting): Add a reference counter to each object whenever there is a place to reference it, counter +1; When the reference fails, the counter-1

2. Accessibility Analytics algorithm (rearchability analysis): Through a series of "GC Roots" objects as a starting point, search down from these nodes, the path of the search is called the reference chain, when an object to the GC Roots This object is not available when no reference chain is connected, the object that is not available is the one that the JVM determines to be recyclable. Currently, the objects in Java that can be used as GC root are:

1. Objects referenced in the virtual machine stack (local variable table)

2. Objects referenced by static properties in the method area

3. Objects that are normally referenced in the method area

4. Objects referenced in the local method stack (native objects)

  

3. Mark - Clear (mark-sweep):    Mark all referenced objects starting from the root node of the reference, traverse the entire heap memory, clear the unmarked objects (old age)

4. Copy (Copying): The memory space is divided into two equal areas, each time only one area is used. During garbage collection, traverse the current usage area and copy the object in use to another area (young generation)

5. Marking - Finishing (mark-compact) : The first stage marks all referenced objects starting from the root node, and the second stage traverses the entire heap, clearing unmarked objects and "compressing" the surviving objects into one of the heaps, in order to emit (the old age)

6. Sub-generational (generational collecting)

First we have to clear the structure of the JVM heap memory (such as), you can see a total of 3 areas: young Generation, older generation (old Generation), permanent generation (Permanent Generation, which is the method area)

  

younger generation (young Generation)

  Including an Eden area and 2 survivor zones (from and to), the vast majority of objects were just created when they were allocated in the Eden area (the Eden area is contiguous memory space, so the memory is very fast on it), and the first time when the first Eden Zone is full of space Mino R GC: Reclaims the "Extinct" object, the surviving object is copied to the from memory (to memory is empty at this time), the Eden area is empty, the next time Eden is full, it will trigger the minor GC, reclaim the extinct object, the surviving object is copied to the from memory, when the from memory is full , the GC copies the surviving objects from the from into to memory, emptying the from, which ensures that at least one zone is empty from and to, and that the objects that repeat the above steps will be left in the old age by default. In the young generation of GC only minor GC, the frequency of occurrence is higher (not necessarily the time of Eden Full)

older generation (old Generation)

Storing the minor GC is still alive, when the old memory space will trigger full Gc,full GC frequency is relatively low, old age object survival time is longer, survival marker high

permanent generation (Permanent Generation is the method area )

Used to store static files, such as Java classes, methods, and so on. Persistent generations have no significant impact on garbage collection, but some applications may dynamically generate or invoke some classes, such as Hibernate, in which case a large, persistent generation space is required to store the new class in these runs.

Some common questions to summarize:

Q:JVM What area of garbage is reclaimed by GC?

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

Q:JVM GC How to tell the object can be recycled?

Object does not have a reference

No catch exception occurred at scope

The program executes properly at scope

The program executed the System.exit ()

Unexpected termination of program (killed thread, etc.)

PS: The above is an online answer, I personally think it should be answered from the GC root search, and after the first mark, clean up, still no resurrection of the object is more reasonable

What are the categories of Q:GC strategies?

Reference count: difficult to solve the problem of mutual reference between objects

Accessibility analysis algorithm: Basic all GC algorithms refer to the concept of root search algorithm

Mark - Clear : There will be memory fragmentation

 Replication: The survival rate of the object is very low

Labeling - finishing (mark-compact) Cons: High cost

in order to optimize the recovery of memory, the JVM uses the method of generational recovery, which mainly uses the replication algorithm for the recovery of Cenozoic memory (Minor GC). For the old-age recovery (Major GC), most of them use the labeling-sorting algorithm 

Q: When will an object be GC?

 Introduce minor GC and full GC

Q: What happens if an object in the old age needs to refer to a new generation of objects?

In order to solve this problem, there is a card table in the old age, it is a block of 512byte size. References to new generations of objects in all old eras will be recorded in this table. When performing GC for the new generation, it is only necessary to query the card table to determine whether it can be recycled without querying the entire old age. This card table is managed by a write barrier. Write barrier has brought a lot of performance gains to the GC, although it may cost some, but it's worth it entirely.

Summary: In fact, the GC as long as the understanding of three questions on the line "when", "to What", "" what to Do

  

GC portion of Java notes

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.