Java Garbage Collection Learning notes

Source: Internet
Author: User

(1) In addition to releasing objects that are no longer referenced, the garbage collector also handles heap fragments . The request to allocate a new object may have to increase the size of the heap space, although the free space available is sufficient, but there is no contiguous space in the heap to place new objects. May cause unnecessary "out of memory" errors for the virtual machine.

(2) with the garbage collection heap, one potential drawback is to increase the burden of the program, which may affect the performance of the program . Because the virtual machine needs to track which objects are referenced by the executing program, it also releases the garbage objects dynamically.

(3) The program can call System.GC () to recommend the JVM to collect garbage, but cannot specify whether an object is garbage or not for the garbage collection mechanism. Even if a GC () is called, garbage collection is not done immediately , or even garbage collection is not necessarily performed . All memory allocation and recycling permissions are in the JVM, not in the developer's hands.

You can try:

Publicclass rubbishrelease {//class Finalize method, you can tell the garbage collector what should be done,    This method inherits from the object class.    //the garbage collector calls the Finalize method of an object before it is permanently deleted from the heap. public void finalize () {system. Out.println ( "The Object is going ..."); public static void main (string[] args) {for (int i = 0; i < 100; i++) {//the following objects are constantly created, but these objects are not referenced new rubbishrelease (); new rubbishrelease (); new rubbishrelease (); System.GC (); } System. out.println ( "The program is Over!");}      

Operation Result:

(4) There are many garbage collection algorithms, but any garbage collection algorithm must do two things. First, it must detect the garbage object . Second, it must reclaim the heap space used by the garbage object and return it to the program .

(5) The two basic methods of distinguishing between active objects and garbage are reference counting and tracking .

(6) Reference counting is an early strategy for garbage collection. In this approach, each object in the heap has a reference count. An object is created, and a reference to the object is assigned to a variable, and the reference count of the object is set to 1. When any other variable is assigned a reference to the object, the count is added to 1. When a reference to an object exceeds the lifetime or is set to a new value, the object's reference count is reduced by 1. Any object with a reference count of 0 can be garbage collected. When an object is garbage collected, it references any object count value minus 1. The advantage of this approach is that the reference counting collector can be executed very quickly, interwoven into the program's operation. This feature is advantageous for real-time environments where programs cannot be interrupted for long periods of time. The downside is that the reference count cannot detect loops (that is, two or more objects refer to each other).

(7) The trace collector Tracks the object reference graph starting at the root node. The objects encountered during the tracing process are tagged in some way. At the end of the trace, unmarked objects are inaccessible and are collected. The basic tracking algorithm is called " Mark and Clear," which indicates two phases of the garbage collection process.

(8) The Java Virtual machine's garbage collector may have a strategy for dealing with heap fragments. The two strategies that are typically used to mark and clear collectors are compression and copying . Both of these methods are quick to move objects to reduce heap fragments.

(9) The compression collector Slides the active object across the idle area to one end of the heap, during which a large contiguous idle area appears at the other end of the heap. References to all moved objects are also updated to point to the new location.

(ten) The copy collector moves all active objects to a new area. In the copy process, they are arranged next to each other, which eliminates the original voids in the old area. That is, the idle area. The generic copy collector algorithm is called "Stop and copy." In this scenario, the heap is divided into two regions, using one area at any time. Objects are allocated in the same area until they are exhausted. At this point, the execution of the program is aborted, the heap is traversed, and the object that encounters the activity is copied to another zone. When the stop and copy process is finished, the program resumes execution. The cost of this method is that it takes twice times the size of memory for a given heap, and that is, because at any time only half of it is used.

collection by Generation : divides memory into blocks based on the difference in the lifetime of the object (one garbage collection is a cycle). The Java heap is generally divided into the new generation and the old age, so that according to the characteristics of each era to adopt the most appropriate collection algorithm. In the Cenozoic, every garbage collection found that a large number of objects died, only a small number of survival, then choose the Copy algorithm, only need to pay a small number of surviving objects copy cost can be completed collection. The "Mark and clear" algorithm can be used in older years because of the high survival rate of the object and the lack of additional space to guarantee it.

finalization (Finalize), which is mentioned in the 3rd above: This method is the garbage collector must run before releasing the object. This possible finalization method complicates the work done by any Java Virtual machine's garbage collector. Because the finalization method may "revive" some objects that are no longer referenced (itself or other objects).

Each object in the heap has one of three states : accessible , resurrected , and untouchable. accessible state good understanding. About the resurrected state : It is not reachable from the trace graph that starts at the root node, but may be touched when the garbage collector executes certain finalization methods. Not only the objects that declare the Finalize method, but all the objects go through a resurrected state. The untouchable state marks not only that the object is no longer touched, but also that it cannot be resurrected by any means of finalization. Inaccessible objects no longer affect the execution of the program, and are free to reclaim the memory they occupy.

(14) The object's strong, soft, weak, virtual reference.

Strong reference: If an object has a strong reference, the garbage collector will never recycle it. When there is not enough memory space, the JVM prefers to throw a outofmemoryerror error, which causes the program to terminate unexpectedly, and does not randomly reclaim the object with strong references to solve the memory shortage problem.

Soft reference: If an object has a soft reference. If there is enough memory space. The garbage collector does not recycle it. If there is not enough memory, the memory of these objects is reclaimed. The object can be used by the program as long as it is not reclaimed by the garbage collector. Soft references can be used to implement memory-sensitive caches.

Weak references: If an object has a weak reference. When the garbage collector finds that only a weak reference object is present, its memory is reclaimed regardless of the current memory space. However, because the garbage collector is a low-priority thread, objects that have only weak references are not necessarily discovered soon.

Virtual Reference: A virtual reference does not determine the life cycle of an object. If an object holds only virtual references, it can be reclaimed by the garbage collector at any time, just as there are no references.

Java Garbage Collection Learning notes

Related Article

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.