Troubleshooting High Java Memory Usage in Spring Boot Microservices

This article documents a production incident where multiple Spring Boot services each consumed around 12 GB of memory, explains how to diagnose the issue using jps and jmap, details default JVM heap size calculations, and provides recommendations for configuring JVM parameters to prevent excessive memory usage.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Troubleshooting High Java Memory Usage in Spring Boot Microservices

In a production environment with a 64 GB server, several Spring Boot microservices began to consume nearly 12 GB of memory each, causing the applications to become unresponsive.

Background

After a period of stable operation, increased business load revealed that each Java application was using an excessive amount of memory. The author reproduced the situation locally to investigate.

The memory usage chart shows the server’s RAM being almost fully utilized, prompting the question of why Java processes were consuming so much memory.

Investigation Steps

step1: Use jps to list Java process IDs or the top command.

step2: Run jmap -heap <processID> to inspect heap configuration.

The jmap output shows a maximum heap size of 4 GB for the test, while the production configuration allowed a maximum heap of 12 GB. -Xmx: maximum heap size, typically about 1/4 of physical memory. -Xms: initial heap size, typically about 1/64 of physical memory.

Oracle’s official JVM defaults are explained: the default maximum heap is half of physical memory for machines ≤ 192 MB, otherwise one‑quarter; the initial heap is at least 8 MB or 1/64 of physical memory up to 1 GB. The young generation is roughly one‑third of the total heap.

Server‑side JVM defaults can be much larger: on 32‑bit JVMs with ≥ 4 GB RAM the default max heap can reach 1 GB, and on 64‑bit JVMs with ≥ 128 GB RAM it can reach 32 GB.

Root Cause

The high memory consumption was caused by launching Spring Boot services without explicit JVM parameters, thus relying on the default settings which allocated overly large heaps for the workload.

Post‑mortem Recommendations

General approach to diagnosing excessive memory usage:

Check JVM parameters: Ensure appropriate -Xms and -Xmx values are set for the application and server resources.

Monitor memory usage: Use monitoring tools or OS utilities to observe heap, non‑heap, and GC activity.

Analyze GC logs: Identify frequent or long garbage collection pauses.

Set reasonable heap sizes: Align heap size with actual application needs and available physical memory.

Use profiling tools: Tools such as VisualVM or MAT can reveal memory leaks or large object allocations.

Running Spring Boot without tuned JVM options can lead to memory overflow, unnecessary garbage collection, and wasted server resources. Properly configuring JVM parameters improves performance, stability, and resource efficiency.

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.

JavaJVMMemory ManagementBackend DevelopmentSpring Boot
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.