Java---garbage collection

Source: Internet
Author: User
Tags instance method

The garbage collection mechanism has the following characteristics

1. The garbage collection mechanism is only responsible for reclaiming objects in heap memory and does not reclaim any physical resources.

2. The program cannot accurately control the operation of the garbage collection, and the garbage collection will take place at the appropriate time. When the object loses its reference permanently, the system will reclaim the memory it occupies in the appropriate time.

3. Before the garbage collection mechanism reclaims any objects, it will always call its finalize () method, which may cause the object to be resurrected (having a reference variable re-referencing the object), causing the garbage collection mechanism to de-recycle.

While an object is running in heap memory, it can be divided into three states according to the state it refers to in the referenced variable:

1. Up to state: When an object has a reference variable reference.

2. Recoverable state: When an object no longer has any reference variable referencing it, it enters a recoverable state. In this state, the system's garbage collection mechanism prepares to reclaim the object's memory, and the system invokes the Finalize () method of all recoverable state objects before recycling. If a reference is called to re-reference the object, the object becomes unreachable, otherwise it is unreachable.

3. Unreachable state: When an object loses all reference variables, and the system calls the Finalize () method and still does not make the object reachable, the object will permanently lose its reference and eventually become unreachable. The system will then actually reclaim the resources that the object occupies.

An object can be referenced by a local variable of a method, or it can be referenced by a class variable of another class, and when the class is destroyed, the object enters the recoverable state. It can also be referenced by an instance variable of another object, which enters the recoverable state only if the object of that instance variable is destroyed.

There are two ways to enforce a system garbage collection:

1. Call the GC () static method of the System class: System.GC ();

2. Call the Runtime object GC () instance method: Runtime.getruntime (). GC ();

The Finalize () method has the following four characteristics:

3. When the JVM executes a finalize () party of a recoverable object, it may make the object or other object reachable to a state.

4. When the JVM executes the Finalize () method exception, the garbage collection mechanism does not report and the program continues execution.

For most objects, there will be a reference variable in the program that references the object. There are four ways to reference objects in Java:

1. Strong references: The most common way to cite. Creates an object and assigns the object to a reference variable. It is in a state of reach and cannot be reclaimed by the garbage collection mechanism.

2. Soft reference: It needs to be implemented by the SoftReference class. When an object has only soft references, it may be reclaimed by the garbage collection mechanism. When the system has enough memory space, it is not reclaimed by the system, and the program can use the object. When system memory is low, the system may recycle it. Soft references are typically used in memory-sensitive programs.

3. Weak references: Weak references are implemented through the WeakReference class, soft references are similar to weak references, but the reference level of weak references is lower. For objects that have only weak references, when the garbage collection mechanism is running, the memory that the object occupies will always be reclaimed, regardless of whether the system's memory is sufficient.

4. Virtual references: Virtual references are implemented through the Phantomreference class, and virtual references are exactly similar to no references. Virtual references are primarily used to track the state of objects being garbage collected, virtual references cannot be used alone, and virtual references must be used in conjunction with reference queues.

The reference queue is represented by the Java.lang.ref.Reference class, which is used to hold references to objects that have been recycled. When you use a soft reference, a weak reference, and a reference queue, the system will add references to the referenced queue after the referenced object is reclaimed, unlike soft references and weak references, where the virtual reference is added to its associated reference queue before the object is freed.

Java is through the accessibility analysis algorithm to determine whether the object is alive, the idea of this algorithm is through a series of objects called "GC Roots" as a starting point, starting from these nodes to search down, search through the path called the reference chain, when an object to the GC Roots no reference chain connected, It proves that the object is not available. In the Java language, objects that can be used as GC roots can include the following:

The object referenced in the local variable table of the virtual machine stack??

? Object referenced by class static property in method area

    • Objects referenced by constants in the method area
    • ?
    • The object referenced by JNI (that is, the general native method) in the local method stack

Even objects that are unreachable in the Accessibility analysis algorithm are not "dead", and they are being recycled at least two times during the tagging process: If the object finds no reference chain connected to the GC roots after the accessibility analysis, it will be first labeled and filtered, The criteria for filtering is whether this object is necessary to perform a finalize () method. When the object does not overwrite the Finalize () method or the virtual machine has already called the Finalize () method, the virtual machine treats both cases as unnecessary execution.

If the object is judged to be necessary to execute the Finalize () method, then the object will be placed in a queue called F-queue and then executed by a low-priority finalizer thread that is automatically created by a virtual machine later. Here the so-called execution refers to the virtual opportunity to trigger this method, but does not promise to wait for it to run over. Because if an object executes slowly in the Finalize () method, the more extreme thing is that a dead loop occurs, which causes all objects in the queue to wait for it, or even the entire memory-recycling system to crash. The virtual machine then marks the F-queue queue for a second time if the object is only re-associated with any of the objects on the reference chain in the Finalize () method. Then in the second token it will be removed from the collection "About to be recycled" and it is really recycled without being removed.

The method area in the Java heap, which is the permanent garbage collection, mainly reclaims two parts: obsolete constants and useless classes. When a constant does not have any object references, it is an obsolete constant. A class that satisfies three conditions can be considered useless: 1. All instances of the class have been reclaimed, that is, no instances of the class exist in the Java heap. 2. The classloader that loaded the class have been recycled. 3. The corresponding Java.lang.Class object of this class is not referenced anywhere, and the method of accessing the class can be accessed anywhere by reflection. The hotspot virtual machine provides control of the-XNOCLASSGC parameters and can also use-verbose:class and-xx:+traceclassloading (product version virtual machine),-xx:+traceunloading ( Version Fastdebug virtual machine) To view information about class loading and unloading.

Java---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.