Java garbage collection mechanism

Usually, the memory space that cannot be reclaimed after we allocate it is called "Memory Leaks".
  
  The potential dangers of the above programming are not allowed in a language such as Java, which is known for its strictness and safety. However, the Java language can neither limit the freedom of programmers to write programs, nor remove the part of declaring objects (otherwise it would not be an object-oriented programming language), so the best solution is to start from the characteristics of the Java programming language itself. Therefore, Java technology provides a system-level thread (Thread), namely the Garbage Collection Thread, to track each allocated memory space. When the Java Virtual Machine (Java Virtual Machine) is in an idle loop, The garbage collector thread will automatically check the memory space allocated by each fast, and then automatically reclaim each useless memory block that can be reclaimed by the fast.
  
  A garbage collector thread is a low-priority thread that has a chance to run only when memory is free during the lifetime of a Java program. It effectively prevents the appearance of memory leaks and saves precious memory resources greatly. However, the implementation of the garbage collector by the Java virtual machine can be various.
  
  The characteristics of the garbage collector and its execution mechanism are described below: the
  
   garbage collector system has its own set of schemes to determine which memory blocks should be recycled and which ones do not meet the requirements and will not be recycled for the time being. The execution of the garbage collector 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, the programmer cannot force the garbage collector to reclaim the memory block. . The only thing the programmer can do is to "advise" the garbage collector to execute by calling the System.gc method, but it is not known whether or when it will execute. This is also the main disadvantage of the garbage collector. Of course, compared with the great convenience it brings to programmers, this disadvantage is not concealed.
  
  Main Features of Garbage Collectors
  1. The working goal of the garbage collector is to reclaim the memory space of objects that have been useless, so as to avoid the generation of memory leaks, save memory resources, and avoid program code crashes.
  2. The garbage collector determines whether the memory space of an object is useless: if the object can no longer be referenced by any "active part" in the program, then we say that the memory space of the object is useless. The so-called "active part" refers to a part of the program that participates in the invocation of the program, is in the process of execution, and has not been executed yet.
  3. Although the garbage collector thread runs as a low-priority thread, it may execute in bursts to save memory resources when the amount of available memory in the system is too low. Of course, its execution is also unpredictable.
  4. The garbage collector cannot be enforced, but the programmer can suggest the implementation of the garbage collector by calling the System.gc method.
   5. There is no guarantee that a useless object will be collected by the garbage collector, nor is there any guarantee that the garbage collector will execute in a piece of Java language code. Therefore, the memory space allocated during the execution of the program may remain until the execution of the program is completed, unless the space is reallocated or reclaimed by other methods. It can be seen that it is impossible to completely eradicate the generation of memory leaks. But don't forget that Java's garbage collector frees programmers from the heavy lifting of manually reclaiming memory space. Suppose a programmer wants to write a code of 100,000 lines of statements in C or C++, then he will fully appreciate the advantages of Java's garbage collector!
  6. There is also no way to predict which of a set of objects that all meet the garbage collector's collection criteria will be collected first.
  7. A circular reference to an object does not affect its collection by the garbage collector.
   8. The garbage collector can be implied to collect an object by initializing its reference variables (handles) to a null value. But at this point, if the object has event listeners attached (a typical AWT component), it still cannot be collected. Therefore, before setting a reference variable to a null value, you should pay attention to whether the object pointed to by the reference variable is being monitored. If so, you must first remove the listener before assigning a null value.
  9. Every 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, such as file handlers and network connectors. The order in which the method is called is independent of the order in which the objects used to call the method are created. In other words, the order in which the methods are written when the program is written is irrelevant to the order in which the methods are actually called. Note that this is only a feature of the finalize( ) method.
  11. The finalize( ) method can only be called once per object. If an exception occurs during the execution of the finalize( ) method, the object can still be collected by the garbage collector.
   12. The garbage collector keeps track of each object, collects those that are not reachable (that is, the object is not called by any "living part" of the program), and reclaims the memory space it occupies. But during garbage collection, the garbage collector will call the finalize( ) method to "recover" the unreachable object as a reachable object by letting other objects know of its existence. Since the finalize( ) method can only be called once per object, it is only possible for each object to "recover" once.
  13. The finalize( ) method can be called explicitly, but it cannot be garbage collected.
  14. The finalize( ) method can be overloaded, but only methods that have the characteristics of the original finalize( ) method can be called by the garbage collector.
  15. The finalize( ) method of a subclass can explicitly call the finalize( ) method of the superclass as the last appropriate action on an object of that subclass. But the Java compiler doesn't consider this an overriding operation, so it doesn't check the call.
  16. When the finalize( ) method has not been called, the System.runFinalization( ) method can be used to call the finalize( ) method and achieve the same effect, garbage collection of useless objects.
  17. When a method is executed, its local variables will go beyond the scope of use, which can be treated as garbage collection at this time, but each time the method is called again, the local variables will be recreated.
  18. The Java language uses a "marked swap area garbage collection algorithm". The algorithm traverses the handle of each object in the program, marks the referenced object, and then recycles the object that has not 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, which will be called before the garbage collector swaps objects for recycling. But don't rely too much on this method to recycle and reuse system resources, because the execution result after this method is called is unpredictable.
  Through the above understanding of the characteristics of the garbage collector, you should be able to clarify the role of the garbage collector and the criteria for the garbage collector to judge whether a memory space is useless. Simply put, when you assign null to an object and redirect the object's referrer, the object is eligible for the garbage collector.
   Judging whether an object complies with the garbage collector's collection standards is an important test site for the garbage collector part of the SUN programmer certification exam (it can be said that this is the only test site). Therefore, candidates in a given piece of code should be able to determine which objects meet the criteria for garbage collector collection and which ones do not. The following is a detailed explanation of the types of questions that may appear in several certification exams:
  Object obj = new Object ( ) ;
   We know that obj is a handle to Object. When the new keyword appears, memory space is allocated to the newly created object, and the value of obj is the first address of the newly allocated memory space, that is, the value of the object (please note that the value of the object and the content of the object have different meanings two concepts: the value of the object refers to the first address of its memory block, that is, the handle of the object; and the content of the object is its specific memory block). At this time, if obj = null; then the memory block pointed to by obj is useless at this time, because the variable is not called again.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327001537&siteId=291194637
Recommended