Monday night after work, I happily came home, my girlfriend ran to me, broom and mop in hand. Is this gonna hit me again? Did I do something wrong again? My brain is spinning at high speed. Then my girlfriend broke the silence.










Garbage Collection is called Garbage Collection. Garbage is a concept related to computer memory management, where garbage refers to memory space that is not used by programs.






What is garbage collection


When it comes to household chores, we have to get rid of certain things. The civilized one is Danshari, and the simpler one is trash.


In the real world, when we talk about trash, we mean books we don’t read, clothes we don’t wear, etc. In this case, ‘rubbish’ means’ things you don’t use ‘. When we clean the house, we usually do two things. We find the garbage that is not used in the house and throw it away so that we can put some other useful things in it.


Mapping to a computer system is the same, the memory of a computer is also limited, it is impossible to store everything in memory all the time, also need to periodically free up unused memory space. And the things that are stored in the unused memory space are garbage. In a program, garbage collection is the process of finding garbage in memory space and collecting it so that the programmer can reuse that space.






What kind of thing is garbage


As we mentioned earlier, garbage in our life is the stuff we don’t use. But how is the “no” thing determined?


When we do our daily chores, we have many ways to determine whether something can be discarded or not.


Reference counting algorithm



First, when we find a USB cable in the room that doesn’t feel useful, here’s how we decide it’s useless:

1, see if there is a device at home that can use this charging port.

2, look at the home can be adapted to this USB cable adapter.


If there is, then we consider the cable to be useful, otherwise, the USB cable will be marked as junk by us. Waiting to be discarded.


This method, known as reference counting in computer junk phone algorithms, works like this: add a reference counter to an object, increment it every time a reference is made to it, and decrease it by 1 when a reference fails. When garbage collection is performed, it is only necessary to determine whether the object’s reference counter has a value of 0. If the reference counter has a value of 0, it is recyclable.


This is a relatively simple algorithm, this garbage collection is relatively simple.


However, there is a drawback to this method of garbage disposal, which may not be effective, like when we want to throw away a USB cable, only to find that the MP3 can use it, and then we keep the USB cable. When we wanted to get rid of the MP3 player, we found that we had a USB cable to use it, so the MP3 player was kept.


But what if the MP3 and USB cable no one wants to use at all? What if the USB cable and MP3 player were left by a guest who said he no longer needed them?


This is the disadvantage of reference counting, which is that if there are circular reference objects, they will not be recycled.






Accessibility analysis algorithm



Of course, in our daily life, we can judge whether something is useful or not, not just by whether there is something “matching” with him, but also by whether the family still needs it.


So, a more reliable judgment of whether an object is garbage or not, we will take an object and ask all members of the family: do you still need this thing?


If the answer is no, then the item can be discarded. This avoids the embarrassment of having MP3 and USB cables mistakenly reserved.


This way is to judge whether something is useful or not from family members. Rather than judging by the relative relationships between objects.


This method of garbage detection is called reachability analysis algorithm in computer. The basic idea of this algorithm is to start from a series of “GC Root” objects and search down from these nodes. When an object is not connected to the GC Root by any reference chain, Proves that the object is not available.


An item that no one in the family has declared necessary or continued to use. Just like an object, reaching all “GC Root” without reference chain is the same.


In the Java language, objects that can be used as GC Root include the following:


1. Objects referenced in the virtual machine stack.


2. The object referenced by the class static attribute in the method area.


The object referenced by the constant in the method area.


4. Objects referenced by JNI in local method stack.










The Fate of garbage


Under normal circumstances, we do not dispose of the useless things in a home and will not decisively throw them away directly. Sometimes for some commemorative or valuable things will be kept for a period of time, after several cleaning, or feel useless, will be completely thrown away.


In fact, computer garbage collection is the same. Even if an object is found to be “unreachable” after being analyzed by the reachability analysis algorithm, it does not have to be reclaimed.


In general, at least two marking processes are required to declare an object dead:


1. After reachability analysis, an object that does not have a reference chain associated with GC Root will be marked and filtered for the first time. The filter is whether this object needs to execute the Finalize () method. If the object does not override the Finalize () method, or has already executed it. Well, I guess he can recycle it. If it is necessary to execute the Finalize () method, then the object will be placed in the Queue of f-Queue, waiting for execution.


2. The virtual machine will set up a low-priority Finalizer thread to finalize() the objects in the F-queue. If an object can “save” itself in finalize(), it will not be recycled, otherwise, it will be moved into a collection of objects to be recycled.


How can objects “save” themselves in Finalize ()?


The simplest way to do this is to re-create the reference, such as assigning itself to a class variable or member variable of an object.