Uncovering 65% Memory Waste in Spring Petclinic: Causes and Fixes

Through a 30‑minute Apache JMeter load test on the Spring Boot Petclinic sample, we captured heap dumps and used HeapHero to reveal that 65% of memory is wasted due to string duplication, inefficient collections, and poor initialization, offering concrete code‑level recommendations to improve memory efficiency.

FunTester
FunTester
FunTester
Uncovering 65% Memory Waste in Spring Petclinic: Causes and Fixes

Environment

The study used the official Spring Petclinic source code ( https://github.com/spring-projects/spring-petclinic) built with Spring Boot 2.1.4, Java SDK 1.8, Tomcat 8.5.20 and MySQL 5.7.26.

Load‑test configuration

Apache JMeter generated a 30‑minute load test with the following parameters:

Virtual users (threads): 1000

Ramp‑up: 0.01 s per new thread (≈100 threads / s)

Loop count: unlimited (each thread repeats the scenario)

Test duration after ramp‑up: 1800 s

The scenario exercised typical Petclinic operations: adding owners, adding pets, recording visits, searching owners, and updating records.

Memory‑waste measurement

Heap dumps were taken from the running application and analyzed with the HeapHero tool. The analysis indicated that roughly 65 % of the heap was wasted due to inefficient programming practices.

Major waste categories

String duplication – 15.6 % of heap

Inefficient raw‑array usage – 14.6 %

Duplicate raw arrays – 14.3 %

Collection inefficiency – 12.1 %

String duplication

Identical strings were created thousands of times, e.g.:

'Goldi' – 207,481 instances

'Visit' – 132,308 instances

'Banglore' – 75,374 instances

'123123123' – 37,687 instances

'Mahesh' – 37,687 instances

These strings originate from the test script (pet names, visit descriptions, city names). Because strings are immutable, each creation allocates a new object, inflating the heap.

Collection inefficiency

Two patterns contributed most to waste:

LinkedHashSet : 99 % of allocated sets were empty, yet each allocation reserves space for 16 entries. Lazy initialization avoids allocating empty sets.

ArrayList : 68 % of lists contained a single element, but the default capacity reserves space for 10 entries, wasting up to 90 % of the internal array.

Bad‑practice code samples

Direct initialization without null checks:

private LinkedHashSet<String> myHashSet = new LinkedHashSet<>();
public void addData(String key) {
    myHashSet.add(key);
}

Improved lazy‑initialization pattern:

private LinkedHashSet<String> myHashSet;
public void addData(String key) {
    if (myHashSet == null) {
        myHashSet = new LinkedHashSet<>();
    }
    myHashSet.add(key);
}

ArrayList capacity‑aware construction:

// Bad: default capacity (10) is allocated
List<String> list = new ArrayList<>();

// Good: allocate exactly the needed size
List<String> list = new ArrayList<>(1);

Technical implications

In cloud environments memory is often the first resource to become saturated. Reducing heap consumption by 30‑40 % can lower the number of required VM instances, decreasing both compute cost and latency because fewer objects are created and collected.

Conclusion

Applying the identified best practices—avoiding repeated string creation, initializing collections lazily, and specifying collection capacities—significantly reduces heap waste in Java back‑end applications such as Spring Petclinic. The resulting memory savings improve response times and lower infrastructure costs.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMemory OptimizationBackend DevelopmentPerformance TestingSpring BootHeap Analysis
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.