Garbage collection in Java and types of garbage collectors

1. Introduction

If you have used C programming language then you must be aware that it is the responsibility of the programmer to release the used memory at the end of the program. This memory management is very important as a system has limited memory and if not properly managed could lead to memory related errors.

The purpose of a garbage collection is to do dynamic memory management automatically. With the introduction of garbage collector, application developer is freed from the responsibility to match memory allocation and deallocation and take care of dynamic memory management.

Few people may argue that this is an overhead on the system as garbage collector has to run. But garbage collection has huge benefits.

In simple term, Garbage collector (GC) as the name suggests is used to collect garbage i.e. collect unused object and free the memory used by unused object.

2. Responsibility of a garbage collector

The responsibility of a garbage collector is to automatically manage the application’s dynamic memory allocation requests. A garbage collector:

  • provides memory to the application as per request.
  • keeps track of the memory usage of the application.
  • reclaims unused memory of the application.
  • allocates from and gives back memory to the operating system.

3. Types of garbage collectors – a brief discussion

There are four types of garbage collectors.

3.1 Serial Collector

The serial collector uses single thread to perform garbage collection and hence relatively efficient as there is no thread communication overhead. This collector is sufficient for most small applications. Serial collector is best suited for single processor machines because it uses single thread and can not fully utilize the multiple processors.

Serial collector is single thread and best suited for applications with small data sets of up to approximately 100 MB.

This collector can be enabled using the option -XX:+UseSerialGC. For example:

java -XX:+UseSerialGC Test 

3.2 Parallel Collector

The parallel collector is also known as throughput collector. Parallel collector uses multiple threads for garbage collection work and hence it is more efficient and fast in comparison with serial collector.

Parallel collector uses a feature Parallel Compaction to perform major collections in parallel. Parallel compaction performs full GCs in parallel which results in lower overhead and better application performance. Since it is multithreaded, parallel collector can take benefit of multiprocessor hardware.

Parallel collector is intended for applications with medium to large-sized data sets that are run on multiprocessor or multi-threaded hardware.

You can enable parallel collector by using the -XX:+UseParallelGC option. Parallel compaction is enabled by default when option -XX:+UseParallelGC has been specified. Parallel compaction can be disabled by using the -XX:-UseParallelOldGC option.

3.3 Garbage-First (G1) Garbage Collector

The Garbage-First (G1) Garbage Collector is intended for multiprocessor machines with a large memory.

G1 came to replace the Concurrent Mark-Sweep (CMS) collector. It is the default collector on most machines. This collector is designed to scale from small single processor machines to large multi-processor machines. G1 collector is mostly concurrent. On most hardware and operating system configurations, G1 is selected as the default collector. G1 collector can be explicitly enabled using the -XX:+UseG1GC option.

3.4 The Z Garbage Collector

Z garbage collector is low latency garbage collector. It is intended for applications which require low latency. ZGC provides max pause times. ZGC is scalable collector and supports heap sizes from 8MB to 16TB. Z garbage collector can be enabled using -XX:+UseG1GC option.

4. Default Garbage Collector, Heap, and Runtime Compiler values

  1. Garbage-First (G1) Collector is the default garbage collector.
  2. By default initial heap size is 1/64 of physical memory.
  3. By default maximum heap size is 1/4 of physical memory.
  4. HotSpot features a Java byte code interpreter in addition to two different Just In Time (JIT) compilers, client (also known as C1) and sever (also known as C2).

Garbage collectors provide some parameters which can be adjusted according to the need and the application.

5. How to set minimum and maximum heap size for garbage collector?

The minimum heap size that the garbage collector can use can be set using -Xms=<nnn> option.

The maximum heap size that the garbage collector can use can be set using the option -Xmx=<mmm> option.

6. Conclusion

In this tutorial, we discussed garbage collection and types of garbage collectors in brief. Each garbage collector will be discussed in detail later. We also discussed few important options which can be adjusted to change the behavior of the garbage collector. The understanding of garbage collection and garbage collectors is very important if you don’t want to rely on virtual machine to decide things for your application.