On the significance of GC garbage collector in Java and its interaction with GC

The object is created using new, but there is no corresponding delete operation to reclaim the memory occupied by the object. When we finish using an object, we just need to stop the reference to the object: change our reference to point to other objects or 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 does not point 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 machine uses garbage collection to ensure that the referenced objects will be retained in memory, and will release the storage space occupied by objects that are unreachable by any reference in the executing code. This is a strong guarantee -- if an object can be reached along the reference chain starting from the root reference (that is, the reference that can be directly accessed in the executing code), the object will not be recycled.

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

Garbage collection means that we never have to worry about dangling references. In those systems where the programmer can directly control when to delete objects, the programmer can delete an object still referenced by other objects. If the programmer deletes such an object, the references still referencing the deleted object will become suspended because they refer to operations

The memory space considered allocable by the operating system (but actually the space has been released). The system can allocate this allocable space to new objects, so that those references to the space actually get objects that are completely different from what they expect. In this case, when the program uses the values stored in this space and operates as objects to which they do not belong, it may cause unpredictable disasters. Garbage collection solves the problem of suspended references for us, because all objects that are still referenced will not be treated as garbage collection, so the space they occupy cannot be released. Garbage collection also solves the problem of accidentally deleting the same object multiple times -- a problem that can also lead to disaster. Recycling garbage objects does not require our intervention, but recycling garbage will occupy certain system resources. The creation and recycling of a large number of objects will interfere with time critical applications. Therefore, when designing this system, we should 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 put them in a list, we can't create new objects when there is not enough space to create new objects and there are no unreferenced objects. If we let the above list keep references to objects that are no longer needed, we will cause a memory leak. Garbage collection solves many (but not all) memory allocation problems.

Interact with the garbage collector. Although the Java language itself does not have any method 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 enable us to call the garbage collector, request to run all finalizers to be run, or view the current memory status:

  . Public void GC Q: this method requests the Java virtual machine to spend energy recycling objects that are no longer used, so that the memory occupied by these objects can be reused.

  . Public void runfinalization(): this method requests the Java virtual machine to spend energy running the following finalizers: objects that have been found to be unreachable but whose finalizers have not yet been executed.

"Public long freememory(): returns an estimate of available bytes of system memory.

・ public long total memory(): returns the total number of bytes of system memory.

  . Public long maxmemory: returns the maximum number of bytes of system memory available to the Java virtual machine. Long is returned if the operating system has no memory usage restrictions on the Java virtual machine MAX-VALUE. There is no method in Java to set the maximum memory of the system. Generally, the Java virtual machine sets this value through the command line or other configuration options.

To call the above method, we need to use the static method runtime Getruntime to get a reference to the current runtime object. The system class supports static GC and runfinalization methods, which will call the corresponding methods on the current runt ime object; In other words, system GC () and runtime getRuntime(). The GC () method is equivalent.

When calling runtime GC () method, the garbage collector may not be able to free up any additional memory, because there may be no garbage to recycle, and not all garbage collectors can find recyclable objects on demand. Therefore, calling the garbage collector may not have any effect. However, before creating a large number of objects, especially in the time critical applications where garbage collection overhead may affect them, call Runtime.. The GC () method is still desirable. There are two potential benefits of 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 garbage collector running during task execution. The following methods actively release all the space that can be released at runtime:

This method is constantly looping. By continuously calling runfinalization and GC methods, the value of freememory increases continuously. 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 finalization method is called asynchronously by the garbage collector. In some cases, such as when a resource that can be reclaimed by the finalize method is exhausted, it is useful to force as many terminations as possible by calling run finalization. But remember, we can't guarantee that any object waiting to be terminated is using this resource, so runfinalization may not have any effect.

The fullgc approach is too radical for most applications. In special cases where garbage collection is mandatory, the system The garbage collected by a single call of GC method is most of them, even if it is not all available garbage. Therefore, repeated calls will reduce the output rate of garbage collection, and in many systems, these repeated calls have no output.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>