Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept and algorithm of garbage collection in java

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about the concept and algorithm of garbage collection in java, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

1. Commonly used garbage collection algorithm 1.1 reference counting method

For an object A, as long as any object references A, the reference counter of An is increased by 1, and when the reference expires, the reference counter is subtracted by 1. As long as object A's reference counter value is 0, object A can no longer be used.

The implementation of the reference counter is very simple, but there are two serious problems:

Unable to process circular references.

The reference counter requires that each reference is generated and eliminated, accompanied by an addition and subtraction operation, which has a certain impact on the performance of the system.

Therefore, the java virtual machine did not choose this algorithm as the garbage collection algorithm.

1.2 Mark removal algorithm

Label removal is the ideological basis of current garbage collection algorithms. Label cleanup divides garbage collection into two phases: marking and cleaning.

Marking phase: first mark all reachable objects starting from the root node through the root node. Therefore, an object that is not marked is a garbage object that is not referenced.

Cleanup phase: clears all objects that are not marked.

The biggest problem with label removal is the possibility of space debris.

1.3 replication algorithm

The core idea of the replication algorithm is to divide the original memory space into two blocks and use only one of them at a time. During garbage collection, the living objects in the memory being used are copied to the unused memory blocks. After that, all objects in the memory blocks being used are cleared, the roles of two memory are exchanged, and garbage collection is completed.

Advantages of the replication algorithm: if there are a lot of junk objects in the system, the number of living objects that the replication algorithm needs to replicate will be relatively small. Therefore, when garbage collection is really needed, the efficiency of the replication algorithm is very high. And because the object is uniformly copied to the new memory space in the process of garbage collection, it can ensure that the reclaimed memory space is not fragmented.

The disadvantage of the replication algorithm: the replication algorithm needs to divide the memory into two pieces, using only one of them at a time (only half of the real memory is used).

1.4 Mark compression algorithm

The tag compression algorithm makes some optimizations on the basis of the tag removal algorithm. Like the tag removal algorithm, the box office compression algorithm also needs to mark all reachable objects once, starting from the root node. After that, however, it does not simply clean up untagged objects, but compresses all living objects to one end of memory. Then clean up all the space outside the boundary.

This method not only avoids the generation of fragments, but also does not need two identical pieces of memory space.

1.5 Generation algorithm

In the previous introduction of several algorithms, none of the algorithms can completely replace other algorithms, they all have their own advantages and characteristics, according to the characteristics of garbage collection objects, using appropriate algorithms, is a wise choice.

Generation algorithm is based on this idea, it divides the memory interval into several blocks according to the characteristics of the object, and uses different collection algorithms according to the characteristics of each memory interval, in order to improve the efficiency of garbage collection.

Generally speaking, java virtual machines put all new objects into the memory area of the new generation, which is characterized by dying out, and about 90% of the new objects will be quickly recycled, so the new generation is more suitable to use replication algorithms.

When an object survives after several collections, the object is promoted to the memory space of the old days. In the old days, almost all objects survived several garbage collections, so it can be assumed that these objects will be resident in memory for a period of time. If you still use the replication algorithm, you will need to copy a large number of objects, and the cost performance of recycling in the old era is lower than that of the new generation, so this approach is not advisable. According to the idea of generation, we can use mark compression or mark removal algorithm for the old age.

In general, the frequency of recycling in the new generation is very high, but each recovery time is very short, while the frequency of recycling in the old era is relatively low, but it takes a long time. To support high-frequency new generation recycling, virtual machines may use a data structure called card tables.

The card table is a collection of bits, and each bit can be used to indicate whether all objects in an area of the old era hold references to new-generation objects. In this way, in the new generation gc, you don't have to spend a lot of time scanning all the old objects to determine the reference relationship of each object. You can scan the card table first. Only when the mark bit of the card table is 1, do you need to scan the old objects in a given area, and the old objects whose card table is 0 certainly do not contain the references of the new generation objects. In this way, the recycling speed of the new generation can be greatly accelerated.

1.6 Partition algorithm

The partition divides the whole heap space into continuous different cells, and each cell is used independently and recycled independently. The advantage of this algorithm is that it can control the number of reclaimed cells at one time.

In general, under the same conditions, the larger the heap space, the longer the time required for a GC, and the longer the pause. In order to better control the pause time generated by gc, a large memory area is divided into several small blocks, and several cells are reasonably reclaimed at a time according to the directory pause time, instead of the whole heap space, so as to reduce the pause caused by a gc.

two。 Judge accessibility

The accessibility of an object consists of the following three states:

Reachable: this object can be reached starting from the root node

Resurrectable: all references to the object are released, but the object may be resurrected in the finallize () function

Untouchable: if the object's finallize () function is called and is not resurrected, it will go into an unreachable state, and an unreachable object cannot be resurrected, because the finallize () function will only be called once.

In the above three states, it can be recycled only when the object is untouchable.

2.1 resurrection of objects

An object is likely to resurrect itself in the finalize () function. Here is an example:

Public class Demo01 {public static Demo01 obj; @ Override protected void finalize () throws Throwable {super.finalize (); System.out.println ("obj finalize called"); obj = this;} @ Override public String toString () {return "I am can rellive obj";} public static void main (String [] args) throws InterruptedException {obj = new Demo01 (); obj = null System.gc (); Thread.sleep (1000); if (obj = = null) {System.out.println ("obj is null");} else {System.out.println ("obj available");} System.out.println ("2nd gc"); obj = null; System.gc (); Thread.sleep (1000) If (obj = = null) {System.out.println ("obj is null");} else {System.out.println ("obj available");}

Results:

The second time that can rellive obj finalize calledobj is available gcobj is null

As you can see, when obj is set to null, gc is performed, and the obj object is resurrected. Then, release the object reference again and gc it before the object is really recycled. This is because when gc is performed for the first time, although the reference in the system has been cleared before the finalize () function call, as the instance method finalize (), the this reference of the object will still be passed into the method. If the reference is leaked, the object will be resurrected and the object will become reachable. The finalize () function is called only once, and the second time the object is cleared, the object has no chance to be resurrected, so it will be recycled.

Note: the finalize () function is a very bad pattern, and it is not recommended to use the finalize () function to release resources for the following reasons:

The finalize () function may cause reference leakage and inadvertently revive the object.

The finalize () function is called by the system, and the time of the call is not clear, so it is not a good resource release solution. It is recommended to release resources in the try-catch-finally statement.

2.2 reference and palpable strength

In java, four levels of references are provided: strong references, soft references, weak references, and virtual references. With the exception of team strong references, the other three references can be found in the java.lang.ref package. Where FinalReference is the "final" reference, which implements the object's finallize () function.

Strong references are the reference types commonly used in programs. Strongly referenced objects are reachable and will not be recycled. The objects of soft reference, weak reference and virtual reference are soft reachable, weak reachable and virtual reachable, and can be recycled under certain conditions.

Examples of strong references:

/ / here str is a strong reference String str = "aaa"

Strong quotation has the following characteristics:

Strong references can directly access the target object

The object pointed to by the strong reference will not be recycled by the system at any time, and the virtual machine would rather throw an OOM exception than recycle the object pointed to by the strong reference.

Strong references can cause memory leaks.

2.3 soft reference

A soft reference is a reference type with a strong reference and a weaker reference. If an object only holds a soft reference, it will be reclaimed when there is insufficient space. Soft references are implemented using the java.lang.ref.SoftReference class.

The following example shows that soft references are recycled when the system heap is out of memory:

Public class Demo02 {public static class User {public int id; public String name; public User (int id, String name) {this.id = id; this.name = name } @ Override public String toString () {return "User {" + "id=" + id + ", name='" + name +'\'+'}';}} public static void main (String [] args) {SoftReference userSoftReference = new SoftReference (new User (1, "geym")) System.out.println (userSoftReference.get ()); / / first garbage collection System.gc (); System.out.println ("after gc"); System.out.println (userSoftReference.get ()); / / after initializing the array, byte [] b = new byte [1024 * 973 * 7]; System.gc (); System.out.println (userSoftReference.get ()) }}

Run with the parameter-Xmx10m, and the result is as follows:

User {id=1, name='geym'} after gcUser {id=1, name='geym'} null

The above proxy first declares the soft reference of the User object, then performs a garbage collection and finds that the soft reference object still exists; then a large object is allocated, and the system thinks that the memory is tight, so it does a gc, and the soft reference is reclaimed at this time.

Each soft reference can be accompanied by a reference queue. When the reachability of the object changes, the soft reference object will enter the reference queue. Through this reference queue, you can track the recycling of the object. The code example is as follows:

Public class Demo03 {private static class User {public int id; public String name; public User (int id, String name) {this.id = id; this.name = name } @ Override public String toString () {return "User {" + "id=" + id + ", name='" + name +'\'+'}';}} static ReferenceQueue softQueue = null Public static class CheckRefQueue extends Thread {@ Override public void run () {while (true) {if (softQueue! = null) {UserSoftReference obj = null; try {obj = (UserSoftReference) softQueue.remove () } catch (Exception e) {e.printStackTrace ();} if (obj = = null) {System.out.println ("user id" + obj.uid + "is delete") }} / / customize a soft reference class. The purpose of extending the soft reference is to record the User.uid. / / later, you can know which User instance has been reclaimed through this uid field in the reference queue. Public static class UserSoftReference extends SoftReference {int uid; public UserSoftReference (User referent, ReferenceQueue)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report