Talking about the java language garbage collector

xiaoxiao2021-03-06  55

Talking about the java language garbage collector

Guo Yi 01-7-6 02:26:57

-------------------------------------------------- ------------------------------

The garbage collector is a major feature of the Java language difference in other programming languages. It relieves programmers from the heavy work of manually recovering memory space. In the Java Programmer certification exam, the garbage collector is a must, generally can account for about 6% of the total score. However, due to the standard textbook for Sun's Java Programming Language SL-275 course, only a very simple introduction to the content of the garbage collector, and some books on Java technology, such as "Java 2 core technology" ( Core Java 2), "Thinking in Java", "Proficient Java 2", etc., there is almost no content on the garbage collector, or just simply raise two sentences, so many participation in the Java Programmer certification exam Chinese candidates, the score of the garbage collector is 0 points (the author knows a teacher of the China Java training course authorized by Sun, and its examination is divided into 89%, but the part of the garbage collector is also 0 marks). In view of this, the author summarizes the topic of this garbage collector, and I hope to help the advice of the majority of Java technology and the candidates who prepare for certification exams. We know that many programming languages ​​allow the memory space to be dynamically allocated in the program runtime. The way the memory is all diverse, depending on the syntax structure of the language. But no matter which language memory allocation mode, finally returns the start address of the assigned memory block, that is, the first address of the pointer to the memory block. When the memory space that has been assigned is no longer needed, in other words, when the handle points to the memory block exceeds the range of use, the program or its operating environment should be recycled to save valuable memory resources. In C, C or other programming languages, whether it is object or dynamically configured resources or memory, it must be declared by the programmer to generate and recycle, otherwise the resources will consume, resulting in waste or even crashing of resources. But handmade recycling is often a complex and arduous job. Because it is very difficult because it is necessary to pre-determine whether the memory space occupied is very difficult! If a program cannot recover memory space, and when there is no memory space that can be assigned during the program runtime, this program can only collapse. Typically, after we allocate, we cannot recover the memory space called "Memory Leaks". The potential hazard of the above programming is not allowed in Java in rigorous and safe language. However, Java language can neither restrict the freedom of programmer writing programs, and cannot remove part of the declaration object (otherwise it is not an object-oriented program language), then the best solution is from the characteristics of the Java program language itself. Thus, Java technology provides a system-level thread, a garbage collection thread (Garbage Collection Thread) to track each block allocated memory space, when Java Virtual Machine is idle, The garbage collector thread automatically checks the memory space allocated, and then automatically reclaims useless memory blocks that can be recycled. The garbage collector thread is a low priority thread, in the life cycle of a Java program, it only has the opportunity to run when the memory is idle. It effectively prevents the emergence of memory leakage, and greatly saves valuable memory resources. However, the scheme for performing the garbage collector through the Java virtual machine can be varied.

The characteristics of the garbage collector are introduced below and its executive mechanism: the garbage collector system has its own set of schemes to determine which memory block should be recovered, which is not required for the request. The implementation of the garbage collector in a Java program is automatic and cannot be enforced. Even if the programmer can clearly determine that there is a memory already useless, it should be recycled, and the programmer cannot forced garbage collector to recycle the memory block. . The only programmer's only thing to do is to "recommend" execute the garbage collector by calling the System. GC method, but it can be executed, but it is not known. This is also the most important disadvantage of the garbage collector. Of course, this disadvantage is that this disadvantage is not to cover the huge convenience to the programmer. The main features of the garbage collector are: 1. The work goal of the garbage collector is to recycle the memory space of the useless object, thereby avoiding the production of memory leaks, saving memory resources, avoiding the crash of the program code. 2. The garbage collector determines whether an object's memory space is useless: if the object cannot be referenced by any "activity part" in the program, we said that the memory space of the object is useless. The so-called "part of the activity" means the call of some part of the participating program in the program. During the execution process, it has not yet been executed. 3. The garbage collector thread is running as a low priority thread, but when the amount of memory available in the system is too low, it may burst into rescue memory resources. Of course, its execution is also unpredictable. 4. The garbage collector cannot be enforced, but programmers can recommend execution of garbage collectors by calling the System. GC method. 5. You cannot guarantee that a useless object will be collected by the garbage collector, nor guarantee that the garbage collector will be executed in a Java language code. Therefore, the memory space assigned in the program execution may remain until the program is executed, unless the space is reassigned or recycled by other methods. It can be seen that it is impossible to completely initially root the production of memory leakage. But please don't forget that Java's garbage collector has made the programmer from the heavy work of manually recovering memory space. Imagine a programmer to write a 100,000-row statement with C or C , then he will fully understand the advantages of Java's garbage collector! 6. There is also no way to predict which one will be first collected in a group of garbage collector collection standards. 7. The loop reference object does not affect its garbage collector. 8. The garbage collector can be hinted to collect this object by initializing the reference variable of the object to the NULL value. At this time, if the object is connected to an event listener (typical AWT component), it is still not a collected. Therefore, before setting a reference variable as a null value, it should be noted whether the object to which the reference variable points to be listened. If there is, you should first remove the listener before you can assign a null value. 9. Every object has a Finalize () method, which is inherited from the Object class. 10. Finalize () method is used to recover system resources other than memory, just like file processors and network connectors. The order in which the method is called and the order of the object used to call the method is independent. In other words, the order and method of the method and the actual call order of the method and method of the method are not coheed. Note that this is just the characteristics of the femalize () method. 11. Each object can only call the femalize () method once. If an exception is generated when the Finalize () method is executed, the object can still be collected by the garbage collector.

12. The garbage collector tracks each object, collects those who are not reachable (that is, the object is not called any "live portion" that is active, reclaiming its own memory space. However, when garbage collection, the garbage collector calls the Finalize () method, making the non-reachable object "recovered" again by allowing other objects to "recover" as the reachable object. Since each object can only call a femalize () method, each object is only "recovered" once. 13. The Finalize () method can be explicitly called, but it cannot be garbage collection. 14. The Finalize () method can be overloaded, but only the method of the initial femitize () method features can be called by the garbage collector. 15. The finalize () method of the subclass can clearly call the Finalize () method of the parent class as the last appropriate operation of the subclass object. However, the Java compiler does not think this is overriding, so it will not be checked. 16. When the finalize () method has not been called, system. Runfinalization () method can be used to call the femalize () method, and achieve the same effect, garbage collection of useless objects. 17. When a method is executed, the local variables will exceed the scope of use, at which time as garbage collection, the local variable will be recreated whenever the method is called again, where the local variable will be recreated. 18. The Java language uses a "garbage collection algorithm for tag exchange area". The algorithm will traverse the handle of each object in the program, tag the referenced object, and then recycle the object that has not been marked. The so-called traversal can be simply understood as "checking each." 19. The Java language allows programmers to add a Finalize () method for any method, which is called before the garbage collector swaps the recycle object. However, do not excessively depend on the method to recycle and reuse the system resources, as the execution result after the method is called 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 garbage collector determines whether a memory space is useless. Simply put, when you assign a value for an object and reordbound the item of the object, this object is in line with the collection criteria for the garbage collector. Judging whether an object meets the collection criteria for the garbage collector, this is an important test site for the garbage collector part of the Sun programmer certification exam (which can be said, this is the only test point). Therefore, the candidates should be able to determine which object that meets the standard collected by the garbage collector, which does not match. The following is a specific explanation that may appear in several certification exams: Object obj = new object (); we know that OBJ is a handle of Object. When the new key is appeared, the memory is allocated to the memory space, and the value of the OBJ is the first address of the newly allocated memory space, that is, the value of the object (please pay special attention, the value of the object, the content of the object is different ". 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 there is OBJ = NULL; then the memory block points to the OBJ is now useless because the variable is not called below.

Please see the following three types of certified exams: Segment 1: 1. FOBJ = New Object (); 2. FOBJ.METHOD (); 3. FOBJ = New Object (); 4. FOBJ.METHOD (); Q: In this code, the FOBJ of the second line meets the collection criteria for the garbage collector? A: Chapter 3. Because the FOBJ of the third line is assigned a new value, a new object is generated, which is changed to a new memory space, which is equivalent to FOBJ in the first row to assign a null value. This type of question is the simplest in the certification 0 exam. Block 2: 1. Object Sobj = new Object (); 2. Object Sobj = NULL; 3. Object Sobj = new Object (); 4. Sobj = new object (); Q: In this code, the memory space of the second line meets the collection criteria for the garbage collector? A: Chain 1 and 3rd lines. Since the second behavior SOBJ is NULL, the SOBJ of the first line meets the collection criteria for the garbage collector. The fourth line is equivalent to the SOBJ value of NULL, so SOBJ in this third line is also in line with the collection of garbage collectors. If there is an object's handle A, and you use A as a parameter of a constructor, that is, when you assign a value to a null, A does not meet the collection of garbage collectors. Until the new object constructed by the above constructor is assigned, A can be collected by the garbage collector. The block 3: 1. Object aobj = new object (); 2. Object bobj = new object (); 3. Object cobj = new object (); 4. AOBJ = Bobj; 5. AOBJ = COBJ; 6. COBJ = NULL; 7. AOBJ = NULL; Q: In this code, the memory space of the second line meets the collection criteria for the garbage collector? A: Chapter 7. Note that this type is the most difficult question that may be encountered in the certification exam. Row 1-3 creates three objects of the Object class: AOBJ, BOBJ, COBJ line 4: At this time, the handle of the object AOBJ points to BOBJ, so the execution of the row cannot make the AOBJ conform to the garbage collector's collection criteria. 5: At this time, the handle of the object AOBJ points to COBJ, so the execution of the row cannot make the AOBJ conform to the collection criteria for the garbage collector. Line 6: There is still no object to match the collection of garbage collectors at this time. Line 7: Object Cobj meets the collection criteria of the garbage collector because the cobj's handle points to a single address space. At Chapter 6, COBJ has been assigned NULL, but it also points to AOBJ (line 5) at the same time, so COBJ does not meet the collection of garbage collectors. At Chapter 7, the address space pointed to by AOBJ is also given null null, which means that the address space pointed to by COBJ has been completely imparted. So, COBJ finally consistent with the collection of garbage collectors. But for AOBJ and BOBJ, it is still unable to determine whether it meets the collection criteria.

转载请注明原文地址:https://www.9cbs.com/read-117468.html

New Post(0)