Java garbage collection mechanism

Source: Internet
Author: User
Tags event listener

In general, we refer to the memory space that has been allocated but cannot be reclaimed as "memory Leaks".

  The garbage collector thread is a low-priority thread that, in the life cycle of a Java program, has the opportunity to run only when the memory is idle. It effectively prevents the appearance of memory leaks and greatly reduces the potential for valuable memory resources. However, the scenario for implementing a garbage collector through a Java Virtual machine can be varied.

  The following describes the characteristics of the garbage collector and its execution mechanism:

  The garbage collector system has its own set of programs to determine which memory block should be recycled, which is not eligible for temporary recovery. The garbage collector's execution in a Java program is automatic and cannot be enforced, even if the programmer can clearly determine that a piece of memory is useless and should be recycled, and the programmer cannot force the garbage collector to reclaim that block of memory. The only thing programmers can do is by calling System. GC method to "recommend" the execution of the garbage collector, but whether it can be executed, and when execution is not known. This is also the main drawback of the garbage collector. Of course, this drawback is his flaws relative to the great convenience it brings to the programmer. Key features of the garbage collector

  1. The garbage collector's goal is to reclaim the memory space of objects that are already useless, thus avoiding the generation of memory leaks, saving memory resources and avoiding program code crashes.

  2. The standard for the garbage collector to determine whether an object's memory space is useless is if the object is no longer referenced by any of the "active parts" of the program, and at this point we say that the object's memory space is useless. The so-called "part of the activity", refers to a part of the program to participate in the program calls, is executing the process, has not been completed.

  3. Although the garbage collector thread is running as a low-priority thread, it may execute suddenly to save memory resources when the system is too low-available. Of course, its execution is unpredictable.

  4. The garbage collector cannot be enforced, but programmers can call system. GC method to recommend that the garbage collector be executed.

  5. There is no guarantee that a useless object will be collected by the garbage collector or that the garbage collector will be executed in a Java language code. Therefore, the memory space allocated during program execution may persist until the program executes, unless the space is reassigned or reclaimed by another method. This shows that it is impossible to completely eradicate the generation of memory leaks. Don't forget, though, that Java's garbage collector frees programmers from the heavy work of manually reclaiming memory space. Imagine a programmer using C or C + + to write a 100,000-line statement of code, then he will be fully aware of the Java garbage collector's advantages!

  6. There is also no way to predict which one will be collected first in a set of objects that meet the garbage collector collection criteria.

  7. A circular Reference object does not affect its collection by the garbage collector.

  8. You can imply that the garbage collector collects the object by initializing its reference variable (reference variables, the handle handles) to a null value. At this point, however, if the object is connected to an event listener (a typical AWT component), it cannot be collected. Therefore, before setting a reference variable to a null value, you should be aware that the reference variable points to the object is listening, if so, to remove the listener first, then you can assign a null value.

  9. Each object has a finalize () method, which is inherited from the object class.

  10. The Finalize () method is used to reclaim system resources other than memory, like file processors and network connectors. The order in which the methods are called is irrelevant to the order in which the objects used to invoke the method are created. In other words, the order of the method and the actual invocation order of the method are irrelevant when the program is written. Please note that this is only a feature of the Finalize () method.

  11. Each object can only call the Finalize () method one time. If an exception (exception) is generated when the Finalize () method executes, the object can still be collected by the garbage collector.

  12. The garbage collector keeps track of each object and collects the unreachable objects (that is, the object is not called by any "live" part of the program), reclaiming its occupied memory space. In the garbage collection, however, the garbage collector calls the Finalize () method to "revive" the unreachable object to a reachable object by letting other objects know it exists. Since each object can only be called once by the Finalize () method, each object can only be "revived" once.

  13. The Finalize () method can be explicitly called, but it cannot be garbage collected.

  14. The Finalize () method can be overloaded (overload), but only methods with the original finalize () method characteristics can be called by the garbage collector.

  15. The Finalize () method of the subclass can explicitly call the Finalize () method of the parent class as the last appropriate action for that subclass object. However, the Java compiler does not consider this to be an overwrite operation (overriding), so it does not check its invocation.

  16. System when the Finalize () method has not been called. The Runfinalization () method can be used to invoke the Finalize () method and achieve the same effect of garbage collection on useless objects.

  17. When a method executes, the local variable goes out of scope and can be garbage collected, but the local variables are recreated each time the method is called again.

  18. The Java language uses a "garbage collection algorithm for the tag Exchange area". The algorithm iterates through the handle of each object in the program, marks the referenced object, and then reclaims the object that has not yet been marked. The so-called traversal can be simply understood as "checking each one".

  19. The Java language allows programmers to add a Finalize () method to any method that is called before the garbage collector swaps the objects. However, do not rely too much on this method to recycle and reuse system resources because the execution results after the method invocation are unpredictable.

  By understanding the characteristics of the garbage collector above, you should be able to identify the role of the garbage collector, and the garbage collector to determine whether a piece of memory space is useless standard. Simply put, when you assign a value of NULL to an object and redirect the object's reference, the object conforms to the garbage collector's collection criteria.

  Determine if an object meets the collection criteria of the garbage collector, which is an important test point for the garbage collector part of the Sun's Programmer certification exam (This is the only testing center, to say the point). Therefore, candidates in a given code, should be able to determine which object conforms to the garbage collector standard, which does not meet. The following combination of several certification exams may appear in the specific types of questions to explain:

  Object obj = new Object ();

  We know that obj is a handle to object. When the new keyword is present, the newly created object is allocated a memory space, and the value of obj is the first address of the newly allocated memory space, that is, the value of the object (note, in particular, that the value of the object and the object's contents are two concepts of different meanings: The object's value is the first address of its memory block, the , and the object's content is its specific block of memory). If there is obj = null at this point, then the memory block that obj points to is useless at this point because the variable is not called again.

Technology sharing:

This article from the "11247808" blog, reproduced please contact the author!

Java garbage collection mechanism

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