How to Slash Spring Boot Memory Usage by Over 40% for Local Development

This guide explains why Spring Boot applications consume memory, shows how to configure the JVM and Tomcat settings, and demonstrates using Docker‑compose to apply these tweaks, enabling developers to reduce local memory usage by more than 40% and speed up development cycles.

macrozheng
macrozheng
macrozheng
How to Slash Spring Boot Memory Usage by Over 40% for Local Development

When developing basic Spring Boot applications, memory consumption can become a nightmare, especially when running multiple micro‑services locally. While Spring Boot and the JVM have sensible defaults, adjusting certain configurations can dramatically lower memory usage during development.

Who Consumes Memory

The JVM is the primary consumer, using heap, metaspace, thread stacks, and other areas.

To reduce memory consumption, we need to pass explicit parameters to the JVM.

Preparation

Install Docker and docker‑compose.

Use Java 17 (any version from 8 upwards works, except some old Java 8 patches).

Use Spring Boot.

Configuration File

Create a file named dev.jvm.conf with the following content:

# dev.jvm.conf
# Override application properties
SERVER_TOMCAT_ACCEPT_COUNT=3
SERVER_TOMCAT_MAX_CONNECTIONS=3
SERVER_TOMCAT_THREADS_MAX=3
SERVER_TOMCAT_THREADS_MIN_SPARE=1
SPRING_MAIN_LAZY_INITIALIZATION=true

# JVM options
JAVA_TOOL_OPTIONS=-XX:+UseSerialGC -Xss512k -XX:MaxRAM=200m

Then pass these environment variables to Docker containers via docker‑compose:

# docker-compose.yml
services:
  service1:
    image: service1:dev
    env_file:
      - dev.jvm.conf

  service2:
    image: service2:dev
    env_file:
      - dev.jvm.conf

Run docker-compose up and observe the reduced memory footprint.

Detailed Parameter Explanation

SERVER_TOMCAT_ACCEPT_COUNT : Max queue length for incoming connections when all request‑handling threads are busy (default 100).

SERVER_TOMCAT_MAX_CONNECTIONS : Maximum simultaneous connections Tomcat can handle (default 8192).

SERVER_TOMCAT_THREADS_MAX : Upper limit for Tomcat request‑handling threads (default 200).

SERVER_TOMCAT_THREADS_MIN_SPARE : Minimum spare threads for embedded Tomcat (default 10).

SPRING_MAIN_LAZY_INITIALIZATION : When set to true, all beans are lazily initialized, shortening startup time.

JAVA_TOOL_OPTIONS : -XX:+UseSerialGC: Use a single‑threaded garbage collector. -Xss512k: Limit each thread stack size to 512 KB. -XX:MaxRAM=200m: Cap JVM RAM usage at 200 MB.

Conclusion

By properly configuring the JVM and Spring Boot and understanding where memory is consumed, developers can significantly reduce local memory usage and improve development efficiency.

Original article translated from Medium .

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.

JVMDockerMemory OptimizationConfigurationSpring Boot
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.