This is the second day of my participation in Gwen Challenge
Why do we need to know about garbage collection?
If you've learned traditional programming languages like C/C++, you know how painful it can be to reclaim allocated memory because you don't know when you'll need it.
And, more painfully, if you don't reclaim memory in time, your system can slow down or even crash, a phenomenon known as a memory leak.
This kind of traditional programming language display garbage collection mechanism has the following two main shortcomings: 1. The program forgot to collect garbage in time, resulting in memory leakage, so that the performance of the system. 2. The program retrieves the system core memory incorrectly, causing the system to crash.Copy the code
One of the obvious advantages of the Java language’s garbage collection mechanism is that it doesn’t require a programmer to do garbage collection manually; the Java virtual machine does it for you! (Isn’t it fun?)
So what is recycling?
Garbage collection is a dynamic storage management technology. It automatically releases the objects that are no longer referenced by the program. According to the specific garbage collection algorithm, memory resources can be automatically reclaimed.
In fact, garbage collection defragment memory in addition to freeing objects that are no longer used. Memory fragmentation occurs as objects are created and the garbage collector frees up the internal space occupied by discarded objects, and Java’s garbage collection mechanism defragmentates these fragments to the other side of the heap, allocating the defragmenting memory to new objects.
However, the Java garbage collection mechanism also has a disadvantage, which can affect system performance, because the Java virtual machine must track useful objects in the program to determine when objects are useless and then recycle them.
Finally, Java programmers can’t force garbage collection. You can set the object’s reference to NULL to indicate that the Java virtual machine is useless, and you can call System.gc() to suggest garbage collection. But when the system does garbage collection is beyond the control of the Java programmer.
So one of the rules of Java writing is: don’t keep references to an object when you don’t need it anymore, or you’ll run out of memory and your system’s performance will deteriorate.
Note: Part of the definition of this article comes from Crazy Java Handout by Li Gang.Copy the code
If the above content is not correct, welcome to comment.