Articles

Spring Boot Docker memory: a 256 MB vs 512 MB test on Gregale

Deploy a Java 21 Spring Boot API with a small Docker runtime and Actuator readiness, then compare 256 MB and 512 MB with the same image and HTTP checks.

Updated

A minimal Java 21 Spring Boot API passed startup and served a request at both 256 MB and 512 MB on Gregale. Size your own container from total memory under load, not from its Java heap alone. The heap sits alongside class metadata, thread stacks, JIT code, direct buffers and native libraries. A JVM with -Xmx96m can use more than 96 MB of container memory.

This is a deployment test for one small Java 21 API, not a memory floor for every Spring Boot app. For Maven, Gradle and PostgreSQL setup, see Deploy a Spring Boot app.

View the source on GitHub or download the runnable sample. It uses Spring Boot 4.2.0, Spring MVC and Actuator. It does not use a database, background workers, or a large thread pool.

Give Spring the host port and a readiness endpoint

properties
server.port=${PORT:8080}
server.address=0.0.0.0
management.endpoints.web.exposure.include=health
management.endpoint.health.probes.enabled=true
management.endpoint.health.show-details=never

Gregale sets PORT; EXPOSE 8080 describes the container, but it does not make Spring listen there. The example uses a separate application readiness path, /actuator/health/readiness, not just proof that a TCP port is open. Actuator's default readiness group does not check every external dependency. If your API needs a database, decide which dependency should block readiness and test that behavior separately.

yaml
hosting:
  health: /actuator/health/readiness

Build a small runtime, and reserve room outside the heap

The Dockerfile builds the jar with Maven and uses jlink to include only the needed Java modules in the final image. The process runs as a non-root user, with a fixed heap cap and Serial GC:

dockerfile
ENTRYPOINT ["/opt/java/bin/java", "-Xmx96m", "-XX:+UseSerialGC", "-XX:+ExitOnOutOfMemoryError", "-jar", "/app/app.jar"]

The smaller image helps meet filesystem limits. It is not proof of smaller memory use. If your app adds a library and logs NoClassDefFoundError for a Java platform class, check the runtime's module list before increasing RAM.

Reproduce the check

The sample directory contains a Dockerfile, source code, Maven configuration and gregale.yaml. To check a container locally, start Docker and run:

bash
docker build --platform linux/amd64 -t spring-docker-guide .
docker run --rm --memory=256m -p 8080:8080 -e PORT=8080 spring-docker-guide

In a separate terminal:

bash
curl -fsS http://localhost:8080/actuator/health/readiness
curl -fsS http://localhost:8080/greet/world

To reproduce the 256 MB configuration on Gregale, sign in with the CLI and use a new app name:

bash
gregale deploy --path . --name YOUR_TEST_APP --create-only
gregale app scale YOUR_TEST_APP --ram 256 --cpu-millicores 1000
gregale deploy --path . --name YOUR_TEST_APP

Repeat for a second disposable app with --ram 512 if your plan allows both sizes. Keep CPU, source and JVM options fixed. Confirm the app settings after deployment so you don't compare two runs at the same memory cap.

To check a private Gregale deployment, use the exact deployment UUID shown on the release page. These commands make authenticated requests to the app and don't open it to the public:

bash
gregale release verify DEPLOYMENT_UUID --app YOUR_TEST_APP --path /actuator/health/readiness
gregale release verify DEPLOYMENT_UUID --app YOUR_TEST_APP --path /greet/world

The first endpoint should report {"status":"UP"}. The second should return {"message":"Hello, world!"}. Validate both. A successful image build alone does not prove the process started or that your route handles a request.

What passed on Gregale

On December 1, 2026, the same built image ran in two disposable Scale apps. Both had the same 1,000 millicore CPU setting and a 96 MiB maximum heap. The table reports configured limits, not measured memory consumption.

Configured RAMApplication readinessApplication response
256 MBHTTP 200, {"status":"UP"}HTTP 200, {"message":"Hello, world!"}
512 MBHTTP 200, {"status":"UP"}HTTP 200, {"message":"Hello, world!"}

256 MB was the smaller successful setting tested, not the minimum for this image. These owner-authorized checks did not measure throughput, peak memory, cold-start latency, or a Free-account deployment. An app with JDBC, more threads or larger payloads may need more headroom.

Diagnose the failure before raising memory

SymptomWhat to check
Build exceeds the filesystem limitCheck the size of the unpacked runtime and jar, not just registry download size.
Build succeeds, startup says the port never openedCheck PORT, the bound address, and whether the Java process exited.
HTTP works but readiness is 404Check Actuator is installed and the probe group is enabled. Also check the configured health path.
Java logs an OutOfMemoryErrorIdentify whether it was heap, direct buffer, or another JVM allocation.
The platform reports an OOM killCheck total instance memory, not only the heap. Leave room for native memory and test under your real traffic.

References: Spring Boot Actuator, Java 21 JVM options and Gregale container compatibility.

Questions

Is the Java heap cap the same as container memory?

No. The container also needs room for JVM metadata, thread stacks, JIT code, buffers and native libraries. Measure the total with your actual workload.

What port should Spring Boot listen on in Docker?

On Gregale bind to 0.0.0.0 and read the PORT environment variable. The sample falls back to 8080 for local Docker runs.

Which Spring Boot readiness endpoint should Gregale check?

Enable Spring Boot Actuator probes and configure hosting.health as /actuator/health/readiness. Decide separately which external dependencies should block readiness.