Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

It is crucial to analyze the memory consumption to improve the Java application performance and avoid memory problems. Since Java application allocates objects in the JVM heap, monitoring the heap memory and analyzing the objects hosted is very important.

In this tutorial, we’ll introduce two different memory analyzers to demonstrate how we analyze the Java heap memory via an offline heap dump file.

2. Capture Heap Dump

A heap dump is a snapshot of all objects contained in the heap at a specific time point. We could create a heap dump file using certain tools such as VisualVM.

VisualVM is a free profiling tool for Java which bundled with JDK up to version 8. It’s distributed as a standalone application after JDK 8.

We need a program that allows VisualVM to capture the heap dump. Let’s create a sample program to leak memory deliberately:

public class MemoryLeakDemo {
    private static final List<InputStream> leakList = new ArrayList<>();

    public static void main(String[] args) throws InterruptedException {
        int counter = 0;
        while (true) {
            byte[] chunk = new byte[1024 * 1024];
            ByteArrayInputStream bais = new ByteArrayInputStream(chunk);
            leakList.add(bais);

            counter++;
            System.out.println("Allocated " + counter + " MB");
            Thread.sleep(100);
        }
    }
}

This program contains a static variable leakyList. This means it would never be garbage collected. The main method has a loop where it continuously puts 1MB of memory into this static list, which will cause a memory leak.

We are ready to go, and we can run VisualVM to profile it. Once we launch VisualVM, we can see a list of Java processes that are running on the left-hand side “Applications” tab:

visualvm 01

Now, we can run the sample program with that, and we’ll see the corresponding Java process show up.  Select the process and switch to the “Monitor” tab on the right panel. We would see the chart of heap memory consumption. Go ahead and click the “Heap Dump” button to generate the heap dump snapshot.

We can export that snapshot as a .hprof file for offline analysis. A .hprof file is in binary format that requires an analyzer to read.

3. Analyzing the Heap Dump with VisualVM

VisualVM has the capability of exporting the heap dump file for offline analysis as well.

When we open up the heap dump file, VisualVM presents a summary of the heap dump that contains general information such as the JVM version and a listing of the environment variables (under “System Properties”):

visualvm 03

The most informative parts are the “Classes by Number of Instances” and “Classes by Size of Instances”. The first one shows the top 5 classes with the most instances created, while the second one shows the top 5 classes consuming the most heap memory.

If we switch our view from “Summary” to “Objects”, we can get the objects residing in the heap memory. We’ll see the origin of the object creation. In the  example below, we can see that the byte array was created in the MemoryLeakDemo:

visualvm 05

4. Analyzing the Heap Dump with Eclipse Memory Analyzer Tool (MAT)

Even though VisualVM can perform basic heap inspection, Eclipse MAT is capable of generating more comprehensive reports on memory analysis.

Once we have imported the heap dump file into Eclipse MAT, we can read multiple reports via the “Overview” tab, such as “Top Consumers” and “Top Components”:

eclipse mat 01

The most helpful one for detecting memory leaks is the leak suspects report. It generates an analysis report for us that finds out all potential memory leaks in the heap dump: eclipse mat 02

By reading the report details, we can trace where the objects were created, similar to what we saw in VisualVM:

eclipse mat 03

In addition to these comprehensive reports, Eclipse MAT supports Object Query Language (OQL), which is a SQL-like language to query against the heap dump.

Let’s issue a sample query to select all the byte arrays that are over a size of at least 1MB:

SELECT *
FROM byte[] obj 
WHERE (obj.@length >= 1048576)

The query result shows there are 715 byte arrays, each with at least 1MB in size. This count aligns with the leak suspects report findings:

eclipse mat 04

6. Conclusion

Memory analysis is the key to optimizing Java application performance. In this article, we have explored how to capture the heap dump and export it as an offline file.

We also looked into various memory analyzers, such as VisualVM and Eclipse MAT, to identify memory leaks.

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW