SoFunction
Updated on 2025-03-09

A brief analysis of the significance of GC garbage collector in Java and its interaction with GC

Objects are created using new, but there is no corresponding delete operation to recycle the memory occupied by the object. When we complete the use of an object, we just need to stop the reference to that object: change our reference to point to other objects or to null; or return from the method so that the local variables of the method no longer exist, so that the reference to these local variables becomes not pointing to any object. Objects that are no longer referenced are called garbage. The process of finding and recycling these objects is called garbage collection o

Java virtual machines use garbage collection to ensure that the referenced objects will be retained in memory, and will also free up the storage space occupied by objects that are unreachable through any reference in the execution code. This is a strong guarantee - if the object is not recycled if the reference chain starting from the root reference (i.e., references that can be directly accessed in the execution code).

In short, when we cannot reach an object from any executable code, the space it takes can be recycled. Note that we use the word "can" because whether the memory space is recycled is determined by the garbage collector. Generally, the garbage collector will only run if more memory space is needed or to avoid memory overflow. However, the program may exit without memory overflow or even without getting close to memory overflow, so it may not require garbage collection at all. In all methods currently executed, if all variables contain references to an object, and starting from these variables, references to this object cannot be found in all domains or array elements along the reference chain, then we say that the object is "unreachable".

Garbage recycling means we never have to worry about dangling references. In systems where programmers can directly control when objects are deleted, programmers can delete objects that other objects are still referencing. If programmers delete such objects, references that are still referencing deleted objects will become void because they refer to the silence.

The system considers it to be allocable memory space (but in fact the space has been released). The system can allocate this allocable space to new objects, so that the references originally pointed to the space actually result in completely different objects than they expected. In this case, unpredictable disasters may occur when the program uses values ​​stored in this space and operates them as objects they do not belong to. Garbage collection solves the problem of hanging references for us, because all objects that are still referenced will not be treated as garbage collection, so the space they occupy cannot be freed. Garbage collection also solves the problem of accidentally deleting the same object multiple times - this problem can also cause disasters. The recycling of garbage objects does not require our intervention, but recycling garbage will occupy a certain amount of system resources. The creation and recycling of large numbers of objects can interfere with time-critical applications, so when designing such systems, we must carefully deal with the number of objects created in order to reduce the amount of garbage to be recycled.

Garbage collection does not guarantee that memory will always have space to create new objects. For example, if we keep creating objects and putting them in a list, we can no longer create new objects when there is not enough space to create new objects and there are no unreferenced objects. If we keep the above list references to objects that are no longer needed, then a memory leak will occur. Garbage collection solves many (but not all) memory allocation problems.


Interact with the garbage collector
Although the Java language itself does not have any way to explicitly dispose of idle objects, we can still find objects that are no longer used by calling the garbage collector directly. Some convenient methods in the Runtime class and system class allow us to call the garbage collector, request to run all the terminals to be run, or view the current memory status:

.public void gc Q: This method asks the Java virtual machine to spend energy recycling objects that are no longer used so that it can reuse the memory occupied by these objects.

.public void runFinalization(): This method asks the Java virtual machine to spend energy running the following finalizers: objects that have been found to be unreachable but whose finalizer has not yet been executed.

"public long freedom(): Returns the estimated number of available bytes in the system memory.

·public long total Memory (): Returns the total number of bytes in system memory.

.public long maxmemoryo: Returns the maximum number of bytes of system memory available to the Java virtual machine. If the operating system does not have memory usage restrictions on Java virtual machines, Long . MAX-VALUE will be returned. There is no method in Java to set the maximum memory of the system. Usually, Java virtual machines set this value through command line or other configuration options.

To call the above method, we need to obtain a reference to the current Runtime object through a static method. The system class supports static gc and runFinalization methods, which will call the corresponding methods on the current Runt-ime object; in other words, the () and the ().gc() method are equivalent.

When calling the() method, the garbage collector may not free up any extra memory, because there may be no garbage to be recycled, and not all garbage collectors can discover recyclable objects on demand. Therefore calling the garbage collector may not have any effect. However, calling the() method is desirable before creating a large number of objects, especially in time-critical applications where garbage collection overhead may affect it. There are two potential benefits to executing it: the first is that we can get as much memory as possible before running the application, and the second is that we can reduce the possibility of the garbage collector running during the execution of tasks. The following method actively releases all space that can be released at runtime:

public static vorememberful1GC(){

Runtime rt=();

long isFree= ();

long wasFree;

do{

wasFree=isFree;

 ();

();

isFreetwo();

}while (isFree>wasFree);

}

The method is constantly looping, and the value of freememory continues to increase by continuously calling the runFinalization and gc methods. When the amount of free memory no longer increases, the loop of the method ends.

We usually don't need to call the runFinalization method because the finalize method is called asynchronously by the garbage collector. In some cases, for example, when a resource that can be recycled by the finalize method is exhausted, it will be useful to enforce as many terminations as possible by calling run-finalization. But remember that we cannot guarantee that any object waiting to be terminated is using this resource, so runFinalization may not have any effect.

The fullGc method seems too radical for most applications. In special cases where forced garbage collection is required, even if not all available garbage is collected by a single call to the method, it is the vast majority of the garbage. Therefore, repeated calls will reduce the output rate of garbage collection, and in many systems, these repeated calls are outputless.