Add to Favorites | Contact us | Chinese
Java certification exams: Quest Java garbage collection
From;    Author:
Garbage collection GC (Garbage Collection) is one of the core technology of Java language, we have devoted over before Java 7 new garbage collector G1 new features, but the internal operation mechanism in terms of JVM, Java's garbage collection principle and the mechanism has not changed. The purpose is to clear the garbage collection of objects no longer in use. GC activity by determining whether the object is an object reference to determine whether the collection of the object. GC must first determine whether the object is time to collect. Two commonly used method is to traverse the reference count and object references. Reference counting collector Reference counting garbage collector in the early strategy. In this method, each object heap (not the reference) has a reference count. When an object is created, and the object is assigned to a variable, the variable count is set to 1. When any other variable is assigned a reference to this object, the count plus 1 (a = b, then the object referenced by b +1), but a reference to an object over the life cycle or is set to a new value , the object reference count by 1. Any object reference count of 0 can be treated as garbage collection. When an object is garbage collected, it refers to any object count by 1. Advantages: reference counting collector can be quickly implemented, intertwined in the program running. Time the program is not interrupted by the real-time environment is favorable. Cons: Can not detect circular references. If a parent has a pair object reference, reference to the parent child object in turn. Thus, their reference count never be 0. Tracking Collector Early in the JVM use reference counting, and now most of the JVM using object reference traversal. Traverse object references a group of objects from the beginning, along the entire object graph on each link can be reached recursively determine the (reachable) objects. If an object can not be a root object from which (at least one) arrives, then it as garbage collection. Traverse the stage in the object, GC must remember which objects can be reached in order to remove the unreachable object, which is called mark (marking) object. Next, GC can not be reached to remove the object. Deleted, some GC simply scanning the stack, delete the unmarked unmarked object, and the release of their memory to generate a new object, which is called the clean (sweeping). Problem with this approach is that the memory will be divided into many small pieces, but they are not sufficient for the new object, but it is a great combination. Therefore, many GC can reorganize the objects in memory and compressed (compact), the formation of available space. To this end, GC need to stop the activities of other activities. This approach means that all the work associated with the application to stop, only the GC run. As a result, a number of changes during the mixed response to the request. In addition, more complex to run GC increase or to reduce or remove the application's interruption. Some GC using a single thread to finish the work, while others are using multiple threads to increase efficiency. Some commonly used garbage collector ◆ tag - Clear Collector The collector traverses the object graph and marked the first accessible object, and then scans the stack to find the object is not marked and released their memory. The collector typically use a single thread of work and stop other operations. And, because it only removed the object that is not marked, but not the object of marked compression, resulting in fragmentation will have a lot of memory, thus wasting memory. ◆ tags - Compression collector Sometimes also called tags - Clear - Compression collector, and the mark - sweep collector has the same mark phase. In the second stage, put the object tag of the new domain, copied to the stack to compress the stack. The collector has to stop other operations. Copying collector The collector will stack into two domains, often called a half space. Use only half the space, JVM generates a new object is placed in the other half space. GC runs, it can reach the object to copy to the other half of the space, thus compressing the stack. This method is suitable for the short lifetime of the object, and continued to copy the long lifetime of the object is less efficient. And for the specified heap size, the need to double the size of the memory, because any time use only half of them. Incremental collector Incremental collector to stack into multiple domains, each time garbage collection from only one domain, but also understood as the stack into little pieces, each time only one block garbage collection. This will result in smaller application downtime, enabling users are generally not aware of the garbage collector is working. Generational collector The disadvantage is that copying collector: each collection, all objects must be copied mark, leading to a long life cycle of some of the objects to be copied back and forth several times, consume a large amount of time. The generational collector can solve this problem, generational collector the stack into two or more fields to store different life objects. JVM generates a new object on which a general domain. Over time, the continued existence of the object (non-short-lived objects) will be used and transferred to a longer life span of the domain. Generational collectors on different domains use different algorithms to optimize performance.
About us | Legal Notices | Sitemap | Links | Partner