Last week, we introduced Java Garbage Collection and its available collectors. Today, we'll discuss how to select the right garbage collectors for your needs and how to utilize them effectively.

Selecting a Garbage Collector

The following chart will help you choose the right GC for your project:

Specification

GC

Option

Applications with small datasets (<= 100MB)

Serial

-XX:+UseSerialGC

Applications running on single processors with no pause time specifications and requirements. 

Serial

-XX:+UseSerialGC

When application performance is required and one second or longer pause time is acceptable. 

Parallel

-XX:+UseParallelGC

When response time is more important than overall throughput and GC, and pause times must be up to approximately one second.

G1 or CMS 

-XX:+UseG1GC 

or -XX:+UseConcMarkSweepGC

When response time is the priority and a large heap is available.

Z GC

-XX:UseZGC

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Weak References

One special thing about Java and GC are Weak References. A WeakHashMap is a special implementation of the map interface, which only stores keys with weak references. This means that when a key is no longer referenced outside the the WeakHashMap, the key-value pair will be garbage collected.

It works pretty much like a HashMap, but there's one exception: if Java memory manager no longer has a strong reference to the key, the entry will be removed from the map.

Best Practices

Overall, one of the most important things to understand is that GC is a non-deterministic process where we can only give hints about when should be a good time for GC to be executed with methods like System.gc() or Runtime.gc(), but there is no guarantee that GC will be executed.

The best way to adjust GC is setting up JVM flags. These flags can adjust GC, initial and maximum heap size, heap section size, and more. This is why it's good to know how the application works and consider the “Selecting a GC” parameters shown above.

That's It, but Not All of It

In summary, knowing how Java Garbage Collector works can help developers create much better applications. Whether you are actually going to choose a GC or not, knowing if you need to choose one is a good start. 

And if the objects are being created when they are really needed and with the scope they’re really needed, congratulations! You know how to maximize GC, and hopefully JVM will be good to you.

 

References

For references and further reading about Java Garbage Collection, please see the following articles:

MANDIC. Java Garbage Collection: melhores práticas, tutoriais e muito mais. Accessed: Dec 4, 2019.

THE URBAN PENGUIN. JAVA Object Lifecycle, de-referencing and garbage collection. Accessed: Dec 4, 2019.

ORACLEa. Java Garbage Collection Basics. Accessed: Dec 4, 2019.

GEEKS FOR GEEKS. Garbage Collection in Java. Accessed: Dec 4, 2019.

ORACLEb. HotSpot Virtual Machine Garbage Collection Tuning Guide. Accessed: Dec 4, 2019.

ORACLEc. Getting Started with the G1 Garbage Collector. Accessed: Dec 18, 2019.

DEVMEDIA. Introdução ao Java Garbage Collection. Accessed: Dec 23, 2019.


Author

Roseane Silva

Roseane Silva is a Java Engineer at Avenue Code. She is very curious about what is going on behind the scenes. She loves reading about core concepts and also likes playing around with tests and databases.


How to Pass the OCP Java SE 11 Certification

READ MORE

How Java Garbage Collection Works and Why You Should Care - Part 1

READ MORE

Introducing JetBrains Academy

READ MORE