Java Interview Question: Analyzing High Memory Usage in Spring Boot Applications and Tuning JVM Parameters
The article documents a production incident where each Spring Boot service consumed about 12 GB of memory on a 64 GB server, explains how default JVM settings caused the issue, and provides step‑by‑step troubleshooting and tuning guidance to reduce memory usage.
In production we encountered Java applications consuming excessive memory; each Spring Boot instance used about 12 GB on a 64 GB server, leading to apparent hangs.
Background
After a period of stable operation, increased traffic caused the memory issue. Because no production screenshots were available, the author reproduced a similar situation locally.
Images show the memory being almost fully utilized.
Solution Steps
step1: Use jps or top to find the process ID.
step2: Run jmap -heap <pid> to inspect heap configuration.
The maximum heap reported was 4 GB, while the production JVM had been configured with a 12 GB max heap.
Maximum heap size ( -Xmx) is usually about one‑quarter of physical memory.
Initial heap size ( -Xms) is usually about one‑sixty‑fourth of physical memory.
Oracle’s official documentation on default JVM parameters is shown below.
Default Heap Size
Unless explicitly set on the command line, the JVM calculates initial and maximum heap based on the amount of RAM.
Client JVM defaults
The default maximum heap is half of physical memory up to 192 MB; otherwise it is one‑quarter of RAM. The initial heap is at least 8 MB or 1/64 of RAM up to 1 GB.
For example, on a machine with 128 MB RAM the max heap is 64 MB; on machines with ≥1 GB RAM the max heap becomes 256 MB.
The JVM only actually uses the maximum heap if the application creates enough objects; the amount allocated at startup (initial heap) is much smaller.
The maximum space for the young generation is one‑third of the total heap.
Server JVM defaults
Server JVM defaults follow the same principles but allow larger values. On a 32‑bit JVM with ≥4 GB RAM, the default max heap can reach 1 GB. On a 64‑bit JVM with ≥128 GB RAM, the default max heap can be as high as 32 GB.
Thus the root cause was that the production team launched Spring Boot without custom JVM options, relying on defaults that allocated an excessively large heap.
Review
General troubleshooting steps for high memory usage include:
Check JVM parameters: Ensure appropriate -Xmx and -Xms values are set for the workload.
Monitor memory usage: Use OS tools or monitoring platforms 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 application demand and server capacity.
Use profiling tools: Tools such as VisualVM or MAT can reveal memory leaks or large objects.
Running with default JVM settings can waste server memory, cause frequent GC, and degrade performance.
Therefore, it is recommended to tune JVM options according to the application’s characteristics and the server’s resources to improve performance, stability, and resource utilization.
Source: juejin.cn/post/7303862023618707491
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
