Java GC Summary

Source: Internet
Author: User

Very early on to Java GC do a little summary, has not time, recently read the Java paper on the Java GC article, feel good, read read, by the way to summarize.

Java paper GC article address, there are a lot of Java technical articles, written well.

1. Introduction to Java GC

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 implement the GC differently, and Oracle currently maintains two Jvm,jrockit Jvm,hotspot JVMs.

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

1. Run-time data area

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

(all from the Internet)


2. Execution engine

JIT Compiler, Garbage Collector


2. Heap Memory

At run time, instances of Java objects are allocated in heap memory. When the object is not referenced, it is marked as eviction by the garbage collector, then reclaimed, and the space occupied in the Heamp is finally released.

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

1. Eden enters the memory area of any instance into the runtime from Eden
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

Contains meta-data information, such as detail information for Class,method


A description of the heap from Java paper:

Eden Space:
Each time an instance is created, it is assigned to the Eden space.

Survivor Space (S0 and S1):
As part of the minor GC, objects are moved from to S0 if they are still referenced.

When a GC is minor, if the object is not referenced, the object is marked as being evicted from the in-memory object.

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 are moved from S1 to old, and the referenced portions in S1 are 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, the memory fragmentation needs to be organized into contiguous space, memory is defragmented, so that the memory space can be allocated quickly.
Call the Finalize () method on the object instance to dispose of the resources held by the object instance before expelling the object instance and recovering the memory space.
Although the Finalize () method must be executed before the memory space is restored, the order is unordered and there is no time limit. 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 such an analogy, is the student 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 easy to understand.

Judge whether the promotion, whether Mark release, can use reference to judge. 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, such as a student just go to school, then he certainly first assigned to primary school, namely Eden.

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

3, if in junior high school also want to continue to study the student, namely still refernced student, will be promoted to the high school S1 carries on the study. That is, dereferenced will be marked as graduating and will be garbage collected.


MAJORGC: (occurs in old)

When S1 after graduating from high school, still want to continue to study, then will be promoted to the university old.

When MAJORGC occurs, the old will reclaim the instances that have been lost, and the references will not be recycled, and continue studying at Old university. When there are too many students in college, it will trigger outofmemoryerror.

(from the Internet in slices)


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 only if it has to be recycled.
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 automatically resets the object to null and is marked as eviction by the compiler.
The compiler will then recycle the objects that are not in use later, before they are run, in advance.

2, a typical example, all properties of an instance are present in the register, the property value of the access instance is read from the register, and if the instance is not written back to the instance in the future, the instance is marked as eviction.

3, the null value is assigned to the instance, and if 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

JVM is a set of standards, there are many implementations, we most often contact the hotspot JVM, for different JVMs, the garbage collector implementation is different.

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

The Heap memory is divided into 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).

Strongly referenced objects are not garbage collected, soft references are not recycled in general, and cannot be recycled 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 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.