Java Garbage Collection - What Is It and How Does It Work New Relic
Java Garbage Collection - What Is It and How Does It Work New Relic
OBSERVABILITY
Garbage collection is a key feature for developers who build and compile Java
programs on a Java Virtual Machine, or JVM. Java objects are created on the
heap, which is a section of memory dedicated to a program. When objects are no
longer needed, the garbage collector finds and tracks these unused objects and
deletes them to free up space. Without garbage collection, the heap would
eventually run out of memory, leading to a runtime OutOfMemoryError.
Java garbage collection helps your Java environments and applications perform
Java applications are compiled into bytecode that may be executed by a JVM.
Objects are produced on the heap (the memory space used for dynamic
allocation), which are then monitored and tracked by garbage collection
operations. Most objects used in Java code are short-lived and can be reclaimed
shortly after they are created. The garbage collector uses a mark-and-sweep
algorithm to mark all unreachable objects as garbage collection, then scans
through live objects to find objects that are still reachable.
Automatic garbage collection means you don’t have control over whether and
when objects are deleted. This is in contrast to languages like C and C++, where
garbage collection is handled manually. However, automatic garbage collection
is popular for good reason—manual memory management is cumbersome and
slows down the pace of application development.
To ensure that garbage collectors work efficiently, the JVM separates the heap
into separate spaces, and then garbage collectors use a mark-and-sweep
algorithm to traverse these spaces and clear out unused objects. Let’s take a
closer look at the different generations in the memory heap, then go over the
basics of the mark-and-sweep algorithm.
The eden space in Java is a memory pool where objects are created. When the
eden space is full, the garbage collector either removes objects if they are no
longer in use or stores them in the survivor space if they are still being used. This
space is considered part of the young generation in the memory heap.
There are two survivor spaces in the JVM: survivor zero and survivor one. This
space is also part of the young generation.
The tenured space is where long-lived objects are stored. Objects are eventually
moved to this space if they survive a certain number of garbage collection
cycles. This space is much larger than the eden space and the garbage collector
checks it less often. This space is considered the old generation in the heap.
So how do these different spaces make garbage collection more efficient? Well,
garbage collection occurs most frequently in the eden space because many new
objects don’t need to stay in memory for very long. However, it wouldn’t make
sense for the garbage collector to keep checking uncollected objects over and
over, particularly if an object needs to remain in the heap for a long time. That’s
an inefficient use of the collector. By moving objects into survivor and tenured
spaces, the garbage collector knows that there is a higher likelihood that the
objects there will need to remain in memory, so it checks those areas less
frequently. Because the tenured space is much larger than the eden space, it fills
up less regularly and the garbage collector doesn’t check it as much. The
potential downside is that the tenured space is more prone to memory leaks
since it isn’t checked as regularly.
The garbage collection cycles in the young generation (eden and survivor
In past versions of Java (before Java 8), there was a third area of memory known
as permanent generation (perm gen or PermGen) that included required
application metadata for the JVM. However, the permanent generation was
removed in Java 8.
Mark-and-sweep
The Java garbage collection process uses a mark-and-sweep algorithm. Here’s
how that works:
When a Java object is created in the heap, it has a mark bit that is set to 0
(false).
During the mark phase, the garbage collector traverses object trees starting
at their roots. When an object is reachable from the root, the mark bit is set
to 1 (true). Meanwhile, the mark bits for unreachable objects is unchanged.
During the sweep phase, the garbage collector traverses the heap,
reclaiming memory from all items with a mark bit of 0 (false).
Increased productivity:
Developers can focus more on application logic and features without worrying
extensively about memory management, leading to increased productivity and
faster development cycles.
Optimized performance:
Minor events: These occur when the eden space is full and objects are
moved to a survivor. A minor event happens within the young area.
Mixed events: These are minor events that reclaim old generation objects.
Major events: These clear space in both the young and old generations,
which takes longer than other types of garbage collection events.
Create an object inside a method. After methods are executed, all objects
called within those methods become unreachable, which makes them
eligible for garbage collection.
Nullify the reference variable. You can change a reference variable to NULL.
As long as all references to an object are removed, that object will become
unreachable, which lets the garbage collector know the object can be
removed.
Understand and adjust garbage collection parameters such as the size of the
Young and Old Generations, thread counts, and collection intervals. This tuning
can significantly impact performance.
Start
monitoring
your Java
R E L AT E D T O P I C S
OBSERVABILITY
Franz Knupfer manages the technical content team at New Relic. Prior
to joining New Relic, he was Director of Curriculum at Epicodus code
school in Portland, Oregon.
The views expressed on this blog are those of the author and do not
necessarily reflect the views of New Relic. Any solutions offered by the
author are environment-specific and not part of the commercial
solutions or support offered by New Relic. Please join us exclusively at
the Explorers Hub (discuss.newrelic.com) for questions and support
related to this blog post. This blog may contain links to content on third-
party sites. By providing such links, New Relic does not adopt,
guarantee, approve or endorse the information, views or products
available on such sites.
R E L AT E D
What is observability?
Read Article
Observability
Read Article
Observability
Read Article
About Us
Careers
Social Impact
Newsroom
Customers
Partner Program
Code of Conduct
Suppliers Portal
Security
ESG
Contact Us
Follow Us
Terms of Service
DMCA Policy
Privacy Policy
Cookie Policy
English