How to Optimize JVM Memory Settings for Spring Boot in Limited Environments

When migrating Spring Boot services to Spring Cloud on servers with constrained memory, understanding the JVM's default heap configuration and applying explicit -Xms and -Xmx parameters, along with using built‑in Java tools to inspect and tune runtime memory, can prevent out‑of‑memory failures.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
How to Optimize JVM Memory Settings for Spring Boot in Limited Environments

Background

During a migration from Spring Boot to Spring Cloud, the number of deployed services grew and the server began to run out of memory. Most services were started without any JVM memory flags, causing each instance to consume 1.5‑2 GB of RAM by default.

JVM Default Memory Settings

If no -Xms or -Xmx options are supplied, the JVM uses its own default strategy, which differs for large‑memory and small‑memory environments.

In a typical 4 GB machine, the defaults are:

The maximum heap is 1/4 of physical memory, and the initial heap is 1/64 of physical memory. If the application exceeds the maximum, an OutOfMemoryError is thrown.

When the physical memory is less than 192 MB (e.g., on a mobile device), the defaults change:

In this case the maximum heap becomes 1/2 of physical memory, while the initial heap remains 1/64 but not less than 8 MB.

Practical Configuration

The simplest way to avoid memory pressure is to set explicit values when launching the jar: java -Xms64m -Xmx128m -jar xxx.jar Note that the memory flags must appear before the -jar option; otherwise they are ignored.

Inspecting JVM Settings

View System Default Memory Settings

On Linux:

java -XX:+PrintFlagsFinal -version | grep HeapSize

On Windows:

java -XX:+PrintFlagsFinal -version | findstr HeapSize

List Running JVM Processes

jps

Typical output shows process IDs and the main class name.

Check GC Statistics for a Specific Process

jstat -gc 51972 5000

The columns include survivor space capacities (S0C, S1C), Eden space (EC, EU), old generation (OC, OU), and GC counts/times (YGC, YGCT, FGC, FGCT, GCT).

Examine Heap Usage with jmap

jmap -heap 10471

The command prints configuration values such as MaxHeapSize, NewSize, OldSize, and detailed usage of young and old generations.

Conclusion

Neglected JVM memory defaults can quickly exhaust server resources when many Spring Boot services run side‑by‑side. By explicitly configuring -Xms and -Xmx, and by using Java’s built‑in inspection tools ( java -XX, jps, jstat, jmap), developers gain fine‑grained control over heap allocation and can prevent out‑of‑memory crashes.

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.

JavaJVMperformanceMemory OptimizationBackend DevelopmentSpring Boot
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

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.