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.
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=200mThen 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.confRun 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 .
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.
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.
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.
