Java GC Concept Summary

Source: Internet
Author: User

For a long time, I would like to make a small summary of the GC in Java, he has no time. According to a recent Java paper up Java GC article, I feel good, read. By the way, summarize.

Java paper GC article address with a lot of Java technical articles. It's all very well written.


1, Java GC Simple Introduction

The JVM's garbage collection mechanism frees developers from taking too much account of memory requests and releases, which reduces the cost of software development and language learning costs.

Different JVMs have different implementations of the GC, and Oracle maintains two JVMs at the moment. JRockit Jvm,hotspot JVM.

The JVM discussed in this article is the JVM that resides in hotspot.

1. Data area at execution time

Method area, heap memory. Java Stack, PC registers, local method stack

(The images below are from the network)


2. Running the engine

JIT Compiler, Garbage Collector


2. Heap Memory

At the time of execution. Instances of Java objects are allocated in heap memory. When the object is not referenced. is marked as eviction by the garbage collector and then recycled. Finally, the space occupied in the Heamp is released.

The Java Heap Memory is divided into three regions:
1. Young Generation

1. Eden enters the memory area from Eden No matter what instance goes into execution.
2, S0 survived a few instances can go from Eden to S0
3, S1 live longer instances from S0 into the S1
2, Old Generation

Tenured (tenure), example from S1 to tenure zone
3, Permanet Generation

Includes meta-data information. such as Class,method's detail information


A description of the heap from Java paper:

Eden Space:
Whenever an instance is created. will be assigned to the Eden space.

Survivor Space (S0 and S1):
As part of the minor GC, objects are assumed to be referenced, moving from to S0.

When minor GC, assuming that the object is not referenced, the object is marked to evict the object in memory.

Old Generation:

The old age is the logical part of the heap memory, and when the garbage collector is minor GC, the surviving object instances in S1 will be moved from S1 to old, and the referenced parts in S1 will be marked as evicted from memory.

Major GC:
Objects in the old age when the major GC is in progress, those objects that are not referenced will be marked as evicted from memory.



Memory Fragmentation:
Memory Fragment: When the instance is deleted from the heap memory, it can be used by the newly allocated object when it is deleted, and the memory fragmentation needs to be organized into contiguous space and memory fragmentation. This allows for high-speed allocation of memory space.


Call the Finalize () method on the object instance to dispose of the resources held by the object instance before the object instance is evicted and the memory space is restored.
Although the Finalize () method must be run before the memory space is restored. But the order is unordered. There is no time limit for the provision. The order of release between multiple instances of an object cannot be determined in advance, and they may even be in parallel.



3. GC process

The relationship between young and old is a promotion.

Lenovo, in fact, can be like this, for example, a student's school scene:

The GC in HEAP memory is a matter of schooling.

We compare these areas to schools . Examples are compared to students .


Young
Eden S0 S1
Elementary and Junior High School

Old
University

Permanet
Student Meta Info


The process of describing GC is very easy to understand.

Infer whether the promotion, whether the mark release, can use reference to infer. So:

Still referenced is still quoted (is still willing to study students)
Dereferenced (a student who is unwilling to study)

MINORGC: (occurs within young)

1, for example, a student just went to school, then he must first assigned to primary school, that is, Eden.

2, assume in the primary school also wants to continue to study the student, namely still refernced's student, will promote to the middle school S0 carries on the study. Students who do not want to learn. That is, dereferenced will be marked as graduating and will be garbage collected.

3, suppose in junior middle School also want to continue to study the student, namely still refernced student, will be promoted to high school S1 to carry on the study. That is, dereferenced will be marked as graduation. will be garbage collected.



MAJORGC: (occurs in old)

S1 After graduating from high school. If you want to continue learning, you will be promoted to Old college.

When MAJORGC occurs, an instance of the lost reference is reclaimed in old. Also quoted will not be recycled and continue studying at Old university. When there are too many students in college, it will trigger outofmemoryerror.

(the image below is from the network)


4. Collection of objects

Under what circumstances will objects be recycled?

Strong Reference not eligible for garbage collection-strongly referenced object instances are not recycled
Soft Reference garbage collection possible but will is done as a last option-the soft-referenced object instance may be recycled. But it must be recycled in the case of recycling.
Weak Reference eligible for garbage Collection-weak references will definitely be recycled
Phantom Reference eligible for garbage Collection-Ghost references will definitely be recycled


1. Objects that have never been used since the declaration. The compiler will voluntarily set the object to null, and it will be marked as eviction by the compiler.
The compiler will then not be able to use the object. Before execution, it is recycled in advance.

2, a typical example, all attributes of an instance are present in the register, and the attribute value of an Access instance is read from the register. Assuming that the instance will not write the property value back to the instance in the future, the instance will still be marked as evicted.



3, the null value is assigned to the instance. Assuming that the instance has no references to other instances, it is tagged for recycling.

4. When the Finalize method is called. The JVM releases all the synchronization locks on that thread.



5. Summary

The JVM is a set of standards. There are very many implementations. The most common thing we touch is the hotspot JVM, which is for different JVMs. The implementation of the garbage collector is also different.

The JVM is primarily divided into execution-time data regions and execution engines.

The Heap memory is classified as Eden. S0,s1. Old,perm

The GC process is the promotion process from Eden to S0,s0 to S1,s1 to old, divided into MINORGC and MAJORGC (FULLGC).

Strong reference objects are not garbage collected, soft references are not recycled under normal circumstances, and cannot be reclaimed until they are recycled.

Both weak references and ghost references are recycled.


--eof--

Original articles, reproduced please specify from: http://blog.csdn.net/oopsoom/article/details/40348125

Java GC Concept Summary

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.