Garbage collection on the JVM (i)

Source: Internet
Author: User
Tags constant garbage collection

Objects created in Java using the new, NewArray, Anewarray, and Multianewarray directives are freed by garbage collection when they are no longer in use. So deserialization is indirectly using one of the previous instructions, and Clone () is a local method?

The JVM specification does not require any specific garbage collection technology, or even a garbage collection mechanism. Probably just say that you do not need to manually release the memory, the specific way to achieve the JVM decision.

The GC, in addition to releasing objects that are no longer referenced, handles the heap fragments, sorting out contiguous free space to get the new object. The total amount of free space is not sufficient, but there are too many fragments to report an "out of Memory" exception.

GC has two benefits: one is to increase productivity, not to be immersed in Memory Link's sometimes even row-by-line check; second, GC is also part of the Java security policy, and it does not lead to JVM crashes due to incorrect release of memory. But a potential flaw in the GC affects the performance of the program, it needs to do something in the background, and there is a lack of real time.

Garbage collection algorithm

GC algorithm to do two basic things: 1. The garbage object was detected; 2. Reclaims garbage objects, releasing the appropriate heap space. Garbage detection is generally the first set up a root object collection, other objects from the root object can be touched is alive, is not reachable is garbage. The identification of the root object here is somewhat fastidious, the views of different JVMs are not exactly the same, but always include the object references in local variables and the stack of stack frames (and references in the class variables).

The source of another root object is an object reference in the constant pool of the loaded class. The string in the constant pool of the class includes the class name, superclass name, Super Interface name, field name, field signature, method name, method signature

Another source is the object reference passed to the method that is not "released" by the local method (depending on the local method interface, the local method can release the reference by simply returning, or explicitly invoke a callback function to release the passed reference or the combination of the two).

Another potential root object source is the portion of the JVM runtime data area that is allocated from the garbage collector's heap. In some implementations, the class data in the method area itself may be stored in a heap using the garbage collector to detect and unload classes that are no longer referenced by using the same garbage collection algorithm as the release object.

In some JVM implementations, like a basic type, such as an int if interpreted as a local pointer, it is pointing to an object in the heap, but the conservative garbage collector does not handle objects in the heap referenced by this base type.

The two basic methods for distinguishing between active objects and garbage are reference counting and tracing.

Reference count Collector

Reference counting is an early policy of garbage collection, which has a reference count for each object in the heap. When an object is created and assigned to a variable, the reference count is 1. Each time you assign a variable, the reference count is plus 1, and the object's reference is reduced by 1 when the object's reference exceeds its lifetime or points to a new value (if the reference is null). Such an object's reference count of 0 o'clock is rubbish and can be cleared. The reference count is powerless for circular references to multiple objects, but these objects are dead, but the reference count is not 0, and there is an additional overhead associated with the increase or decrease in the number of citations, based on which the technology is now unacceptable.

Trace Collector

Trace Collection traces the object reference graph starting from the root node. The basic tracking algorithm is called "Mark and Purge", which is the two stages of garbage collection. Tag phase, the garbage collector traverses the reference tree, marking each object encountered. The purge phase, objects that are not marked are freed. You might set the tag on the object itself, or you could set the tag with a separate bitmap.

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.