×
Community Blog Understanding Garbage Collection in Java

Understanding Garbage Collection in Java

In this article, we will explain the Garbage collection (GC) mechanism in Java via the explanation on GC and how GC works.

Garbage collection (GC), as its name implies, is a means of freeing space occupied by waste materials, or garbage, and avoid memory leaks. Through performing the GC mechanism, available memory can be effectively used. Moreover, through this process, objects that are dead or unused for a long time in the memory heap will be deleted and the memory space used by these objects will be reclaimed.

GC on the Java virtual machine (JVM) follows the Stop-The-World mechanism. Stop-The-World means that the execution of the program is suspended for GC till all objects in the heap are processed.

In Java, GC roots can be four types of objects:

  1. Objects referenced in the virtual machine (VM) stack, that is the local variable table in the stack frame
  2. Objects referenced by class static attributes in the method area
  3. Objects referenced by constants in the method area
  4. Objects referenced by JNI (the Native method) in the native method stack

After determining the garbage to be collected, the garbage collector starts its work. However, this involves a question: How can we efficiently perform GC? JVM specifications do not clearly define how to implement the garbage collector. Therefore, VMs from different manufacturers can implement the garbage collector in different ways.

  1. Mark-Sweep Algorithm: It is the most common garbage collection algorithm, which performs two operations. It first marks the objects to be garbage-collected in the memory space and then clears the marked objects up from the heap. The algorithm has a clear logic and is easy for operation. However, it has a big problem of memory fragmentation.
  2. Copying Algorithm: It evolves from the Mark-Sweep algorithm to solve the problem of memory fragmentation. This algorithm divides available memory into two equally sized semi-spaces. Only one semi-space is active at a time.
  3. Mark-Compact Algorithm: It has the same marking process as the Mark-Sweep algorithm. However, this algorithm does not directly clear up the objects that can be garbage-collected. Instead, it moves all living objects to one end, and then reclaims the memory space beyond the end boundary.
  4. Generational Collection Algorithm: It is not an idea or a theory, but a combination of the first three algorithms. It provides the combination of different algorithms for different scenarios.

For more detailed information, please go to How Does Garbage Collection Work in Java?.

Related Blog Posts

How Can Tens of Millions of Data Records Be Processed Every Second?

In this article, we will look at the system implemented by Alibaba's Xianyu team, which can process tens of millions of data records every second in real time.

To gain a better grasp of the system and how it works, it's important to clarify first what exactly are the system inputs and outputs.

First, the system's inputs include the following: First there's request logs, including the traceID, timestamp, client IP address, server IP address, consumed time, return code, service name, and method name. Then, there's also the environment monitoring data, which includes the indicator name, IP address, timestamp, and indicator value, which may be (for example, the CPU, number of Java Virtual Machine (JVM) garbage-collections (GCs), JVM consumed time (GC), and database indicators).

Now, let's discuss the system outputs: Outputs include the root causes of service errors within a time period. A directed acyclic graph (DAG) is used to display the error analysis result of each service error. The root node is the analyzed error node, while the left nodes are the error root cause nodes. A leaf node may be an externally dependent service error or a Java Virtual Machine (JVM) exception.

Apache RocketMQ™ into the 500,000-TPS Message Club

The Alibaba Cloud messaging team devoted to Apache RocketMQ™ performance optimization has reached new heights in recent times with the latest TPS for medium-sized and small messages in Apache RocketMQ™ by reaching 470,000. The TPS peak, once, detected on F43 model hit 570,000. How you ask? This article will review some of the techniques we used.

Before we get down to performance optimization at the kernel level, we first need to perform the Java-level optimization. First you must ensure that no other interferences, as well as a complete utilization of the features of JVM preheat (JIT) exist. Our recommendation would be the Benchmark tool JMH developed by OpenJDK.

The biggest hurdle impairing the Java application performance is the JVM pause. We are also familiar with the STW (Stop the World) at the GC stage STW, and in addition to GC, there are many other reasons that affect the Java application.

Related Documentation

Use active diagnosis to troubleshoot response time errors

Response time errors are likely caused by the long response time of downstream applications, uneven traffic, an excessive number of full garbage collection (GC) activities, or an excessive load.

Locating and troubleshooting response time errors is a long and complex process. Application Real-Time Monitoring Service (ARMS) provides the active diagnosis feature to help you quickly and accurately locate response time errors. Active diagnosis of ARMS shortens the response time of applications.

JVM monitoring

The application monitoring function of Application Real-Time Monitoring Service (ARMS) provides the Java Virtual Machine (JVM) monitoring function. It monitors heap metrics, non-heap metrics, direct buffer metrics, memory-mapped buffer metrics, garbage collection (GC) details, and the number of JVM threads. This topic describes the JVM monitoring feature and how to monitor JVM metrics.

Related Products

Application Real-Time Monitoring Service

Application Real-Time Monitoring Service (ARMS) is an end-to-end Alibaba Cloud monitoring service for Application Performance Management (APM). You can quickly develop real-time business monitoring capabilities using the frontend monitoring, application monitoring, and custom monitoring features provided by ARMS.

Log Service

Log Service is a complete real-time data logging service that has been developed by Alibaba Group. Log Service supports collection, consumption, shipping, search, and analysis of logs, and improves the capacity of processing and analyzing large amounts of logs.

0 0 0
Share on

Alibaba Clouder

2,599 posts | 762 followers

You may also like

Comments