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 use of JVM garbage collection mechanism

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the use of JVM garbage collection mechanism, the article is very detailed, has a certain reference value, interested friends must read it!

Overview of gc for 1.JVM

Gc, the garbage collection mechanism, means that jvm is used to free the memory occupied by objects that are no longer in use. The java language does not require jvm to have gc, nor does it specify how gc works. However, most jvm have gc, and most gc use similar algorithms to manage memory and perform collection operations.

After a full understanding of the garbage collection algorithm and execution process, its performance can be effectively optimized. Some garbage collection is dedicated to special applications. For example, real-time applications are primarily designed to avoid garbage collection interruptions, while most OLTP applications focus on overall efficiency. By understanding the workload of the application and the garbage collection algorithms supported by jvm, you can optimize the configuration of the garbage collector.

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. The first step of the JVM garbage collection mechanism is to determine whether the object is ready for collection. Two common methods are reference counting and object reference traversal.

1.1. Reference count

The reference count stores the number of all references to a particular object, that is, when the application creates references and the references go out of scope, jvm must increase or decrease the number of references appropriately. Garbage collection can be performed when the number of references to an object is 0.

1.2. Object reference traversal

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.

two。 Several uses of JVM garbage collection mechanism

2.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.

2.2. Tag-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.

2.3. Copy collector

In the JVM garbage collection mechanism, 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.

2.4. Incremental collector

The incremental collector divides the stack into multiple domains, collecting garbage from only one domain at a time. This can cause minor application interruptions.

2.5. Generational collector

This collector divides the stack into two or more fields 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 will be used and transferred to a longer-lived domain. Generational collectors use different algorithms for different domains to optimize performance.

2.6. Concurrent collector

The concurrent collector in the JVM garbage collection mechanism runs at the same time as the application. At some point (such as compression), these collectors generally have to stop other operations to complete specific tasks, but because other applications can perform other background operations, the actual time to interrupt other processing is greatly reduced.

2.7. 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.

These are all the contents of this article entitled "what is the use of JVM garbage collection mechanism?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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