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 Java garbage collection mechanism?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is the Java garbage collection mechanism". In the daily operation, I believe many people have doubts about what the Java garbage collection mechanism is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the Java garbage collection mechanism?" Next, please follow the editor to study!

Garbage collection GC (Garbage Collection) is one of the core technologies of Java language. We have specifically discussed the new features of the new garbage collector G1 in Java 7 before, but in terms of the internal operation mechanism of JVM, the garbage collection principle and mechanism of Java have not changed. The purpose of garbage collection is to clear objects that are no longer in use. GC determines whether to collect an object by determining whether it is referenced by the active object. GC first determines whether the object is ready to be collected. Two common methods are reference counting and object reference traversal.

Reference count collector

Reference counting is an early policy in the garbage collector. In this approach, each object (not a reference) in the heap has a reference count. When an object is created and the object is assigned to a variable, the variable count is set to 1. When any other variable is assigned as a reference to this object, the count is added by 1 (a = b, then the object referenced by b + 1), but when a reference to an object exceeds its lifetime or is set to a new value, the reference count of the object is subtracted by 1. Any object with a reference count of 0 can be collected as garbage. When an object is garbage collected, the count of any objects it references is subtracted by 1.

Advantages: the reference count collector can be executed quickly and intertwined in the program. It is beneficial to the real-time environment where the program is not interrupted for a long time.

Cons: circular references cannot be detected. If the parent object has a reference to the child object, the child object in turn refers to the parent object. In this way, their reference count can never be 0. 0.

Tracking collector

While early JVM used reference counting, most JVM now use object reference traversal. Object reference traversal starts with a set of objects and recursively determines which objects are reachable along each link on the entire object graph. If an object cannot be reached from one or at least one of these root objects, it is treated as garbage collection. During the object traversal phase, GC must remember which objects are reachable in order to delete unreachable objects, which are called marking objects.

Next, GC deletes unreachable objects. When deleting, some GC simply scan the stack, delete untagged and untagged objects, and free up their memory to generate new objects, which is called sweeping. The problem with this approach is that memory is divided into small pieces, which are not enough for new objects, but they are large in combination. As a result, many GC can reorganize objects in memory and compact them to form available space.

To do this, GC needs to stop other activities. This approach means that all application-related work stops, and only GC runs. As a result, many promiscuous requests were added or subtracted during the response. In addition, more complex GC is added or running at the same time to reduce or remove interruptions to the application. Some GC use single thread to do this, while others use multi-thread to increase efficiency.

Some commonly used garbage collectors

(1) Mark-clear collector

This collector first traverses the object graph and marks reachable objects, and then scans the stack for untagged objects and frees their memory. This collector typically uses a single thread to work and stop other operations. And, because it only clears the untagged objects, but does not compress the tagged objects, it will produce a lot of memory fragments, thus wasting memory.

(2) Mark-compression collector

It is sometimes called Mark-clear-Compression Collector, which has the same marking phase as Mark-clear Collector. In the second stage, the tag object is copied to the new domain of the stack to compress the stack. This collector also stops other operations.

Copy collector

This collector divides the stack into two domains, often called a half-space. Only half of the space is used at a time, and new objects generated by JVM are placed in the other half. When GC runs, it copies reachable objects to the other half of the space, thus compressing the stack. This method is suitable for short-lived objects, and continuous replication of long-lived objects leads to reduced efficiency. And for a specified size heap, twice the amount of memory is required, because only half of it is used at any time.

Incremental collector

The incremental collector divides the stack into multiple domains and collects garbage from only one domain at a time, which can also be understood as dividing the stack into small chunks and garbage collection for only one block at a time. This results in less application downtime, so that users are generally not aware that the garbage collector is working.

Generational collector

The disadvantage of the replication collector is that all marked objects are copied each time they are collected, resulting in some objects with a long life cycle being copied back and forth multiple times, consuming a lot of time. The generational collector can solve this problem. The generational collector divides the stack into two or more domains to store objects with different lifetimes. New objects generated by JVM are typically placed in one of these domains. Over time, objects that continue to exist (non-short-lived objects) will gain useful life and be transferred to a longer-lived domain. Generational collectors use different algorithms for different domains to optimize performance.

Parallel collector

Parallel collectors use some traditional algorithm and use multiple threads to perform their work in parallel. Using multithreading technology on multi-CPU machines can significantly improve the scalability of java applications.

Trace collector legend

Attention should be paid to the use of garbage collector

Here are some points to pay attention to about the garbage collector. There is a lot of knowledge about the garbage collector, and only some of the necessary knowledge is listed below:

(1) the finalize () method can only be called once per object. If an exception (exception) is generated when the finalize () method is executed, the object can still be collected by the garbage collector.

(2) the garbage collector tracks each object, collects those unreachable objects (that is, the object is no longer referenced by the program), and reclaims the memory space it occupies. However, during garbage collection, the garbage collector calls the object's finalize () method, if any. If you make the object referenced by the program (commonly known as resurrected) in the finalize () method, the object becomes reachable and will not be garbage collected for the time being. But because each object can only call the finalize () method once, each object can only be "revived" once.

(3) the Java language allows programmers to add a finalize () method to any method, which is called before the garbage collector exchanges reclaimed objects. However, do not rely too much on this method to recover and reuse system resources, because the execution result after the method call is unpredictable.

(4) the garbage collector cannot be enforced, but programmers can recommend garbage collection by investigating the System.gc method. Remember, it's just advice. Generally speaking, it is not recommended to write System.gc yourself, because it will increase the workload of garbage collection.

At this point, the study on "what is the Java garbage collection mechanism" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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