Quick revision
The short answer
Two separate problems cause the loop. On 0.5 vCPU, Spring Boot can take longer to start than the load balancer's health check allows, so ECS replaces healthy-but-slow tasks; we fix that with healthCheckGracePeriodSeconds on the service and a startPeriod on the container health check. Exit code 137 means the container got SIGKILL, almost always for exceeding the 1 GB memory limit with heap plus metaspace, thread stacks, direct buffers and code cache, so we size the heap with -XX:MaxRAMPercentage around 70 to 75 instead of a fixed -Xmx near the limit, check native memory with Native Memory Tracking, set processor count or GC explicitly if needed, and point health checks at Actuator's liveness and readiness endpoints with graceful shutdown.
What to remember
In this answer, we separated a Fargate restart loop into two causes. Slow startup on 0.5 vCPU lets the load balancer declare tasks unhealthy before they are ready, fixed with a grace period on the service and a startPeriod on the container check. Exit code 137 is a SIGKILL from the memory limit, fixed by sizing the heap with MaxRAMPercentage, measuring native memory with NMT, and being aware of the GC and processor defaults a fractional vCPU causes, while Actuator probes and graceful shutdown keep deployments clean.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Two separate problems cause the loop. On 0.5 vCPU, Spring Boot can take longer to start than the load balancer's health check allows, so ECS replaces healthy-but-slow tasks; we fix that with healthCheckGracePeriodSeconds on the service and a startPeriod on the container health check. Exit code 137 means the container got SIGKILL, almost always for exceeding the 1 GB memory limit with heap plus metaspace, thread stacks, direct buffers and code cache, so we size the heap with -XX:MaxRAMPercentage around 70 to 75 instead of a fixed -Xmx near the limit, check native memory with Native Memory Tracking, set processor count or GC explicitly if needed, and point health checks at Actuator's liveness and readiness endpoints with graceful shutdown.
Why this question is asked
A restart loop on Fargate is stressful because it looks like one big failure, while it is usually two small, independent ones that feed each other: slow startup on a small CPU share and a memory limit the JVM does not respect. Interviewers ask this to see whether we can read the signals ECS gives us, understand what the JVM does inside a container, and change the right settings rather than just doubling the task size. In this answer, we will separate the two symptoms, explain why startup is slow and why ECS kills slow tasks, decode exit code 137, size JVM memory properly, look at the CPU count effect, and set up health checks and shutdown correctly.
1Separating the Two Symptoms
ECS records why each task stopped. In the console or with aws ecs describe-tasks, the stoppedReason and the container's exitCode tell us which problem we are looking at. Tasks stopped for health reasons show a message such as Task failed ELB health checks, with an exit code from the SIGTERM and SIGKILL ECS sends during the stop. Tasks killed for memory show exit code 137 and usually a reason mentioning OutOfMemoryError: Container killed due to memory usage.
Exit code 137 is 128 plus 9, where 9 is SIGKILL: something killed the process without asking. Inside a container with a memory limit, that something is almost always the kernel's out-of-memory killer, enforcing the limit.
2Why Startup Is Slow and Why ECS Kills Slow Tasks
Half a vCPU is half of one core's time. Spring Boot startup is CPU-heavy, class loading, bean creation, JIT compilation, so an application that starts in 8 seconds on a laptop may take 40 to 60 seconds on 0.5 vCPU. Fargate has no burst, so that is simply the speed.
Meanwhile the load balancer starts health checking the task as soon as it registers. With a typical check every 30 seconds and two failures marking a target unhealthy, a task that needs 60 seconds is declared unhealthy before it has finished starting, and ECS replaces it. The replacement is just as slow, so the loop never ends.
The fix is to tell ECS to wait. healthCheckGracePeriodSeconds on the ECS service makes it ignore load balancer health results for that many seconds after a task starts, and startPeriod on the container health check does the same for the container-level check. We set both a little above the measured startup time, for example 120 seconds.
8 messages in order. Play them, or step through them one at a time.
A grace period tells ECS to ignore these early failures.
3Where 1 GB Goes Inside a JVM
The memory limit applies to the whole process, not just the heap. A Spring Boot JVM uses heap for objects, and outside it metaspace for class metadata, often 100 to 150 MB for a Spring application, the code cache for compiled code, around 50 MB or more, a stack for every thread, up to 1 MB each, so 200 Tomcat threads can reserve a lot, direct buffers used by Netty and NIO, and GC bookkeeping. If someone sets -Xmx900m on a 1 GB task, the heap alone can grow to 900 MB and everything else no longer fits; the kernel kills the process, and we see 137 without any Java OutOfMemoryError in the logs.
| Area | With -Xmx900m | With MaxRAMPercentage=70 |
|---|---|---|
| Heap | up to 900 MB | up to about 700 MB |
| Metaspace | about 120 MB | about 120 MB |
| Code cache | about 50 MB | about 50 MB |
| Thread stacks and native | about 80 MB | about 80 MB |
| Total | about 1,150 MB -> killed (137) | about 950 MB -> fits |
4Sizing JVM Memory for the Container
Instead of a fixed -Xmx, we let the JVM size the heap from the container limit with a percentage. Modern JDKs read the cgroup memory limit, and -XX:MaxRAMPercentage=70 to 75 leaves the rest for native memory. If native usage is unusually high, we measure it with Native Memory Tracking and adjust.
If the numbers show the service truly needs more, we raise the task memory. But often the fix is just giving the heap a share instead of nearly everything, or trimming thread counts. NMT costs a little performance, so we use summary mode and may turn it off once sizing is settled.
5What 0.5 vCPU Does to the JVM's Choices
With a fractional CPU, the JVM sees one available processor. That changes its defaults: with fewer than two processors or less than about 1,792 MB of memory, it treats the machine as a client-class machine and picks the Serial garbage collector instead of G1, and thread pools sized from availableProcessors(), such as ForkJoinPool's common pool, get one thread. Serial GC can be fine for a small heap, but its pauses stop the whole application.
We can make these choices explicit: -XX:+UseG1GC or keep Serial on purpose, and -XX:ActiveProcessorCount=2 when we want pools sized as if two processors were present. Often the better answer is a task of 1 vCPU, which also halves startup time and may remove the need for a long grace period.
6Health Check Endpoints and Graceful Shutdown
The load balancer should check /actuator/health/readiness, so a task receives traffic only when the application is ready, and the container health check should use /actuator/health/liveness, which fails only when the application is broken and a restart would help. Spring Boot enables these probes automatically on Kubernetes and we turn them on elsewhere with management.endpoint.health.probes.enabled=true. A liveness check should not include the database, or a database outage would restart every task.
For deployments, graceful shutdown lets in-flight requests finish when ECS sends SIGTERM: server.shutdown=graceful, the default since Spring Boot 3.4, plus spring.lifecycle.timeout-per-shutdown-phase below the task's stopTimeout and the target group's deregistration delay set so the load balancer stops sending new requests first.
7Follow-up questions
How would you reduce startup time instead of extending the grace period?
Give the task more CPU, since startup is CPU-bound and Fargate does not burst; 1 vCPU often halves it. Then reduce the work: remove unused starters and auto-configurations, enable lazy initialization where it is safe, and use class data sharing, an AppCDS archive or, on JDK 25, the AOT cache from Project Leyden, which stores loaded and linked classes from a training run. Spring Boot's AOT processing helps further, and a GraalVM native image is the extreme option. We measure each step with the startup time Spring Boot logs.
Why is there no Java OutOfMemoryError in the logs when the task gets 137?
Because the JVM never ran out of heap. The process as a whole exceeded the container limit, and the kernel killed it with SIGKILL, which cannot be caught, so the JVM had no chance to log anything. A Java OutOfMemoryError happens when the heap or metaspace hits its own limit inside the JVM, which is a different problem.
Should the liveness probe check the database?
No. If the database is down, restarting the application does not help and only adds startup load. Database health belongs in readiness, or in neither, depending on whether the service can do anything useful without it.
8Conclusion
In this answer, we separated a Fargate restart loop into two causes. Slow startup on 0.5 vCPU lets the load balancer declare tasks unhealthy before they are ready, fixed with a grace period on the service and a startPeriod on the container check. Exit code 137 is a SIGKILL from the memory limit, fixed by sizing the heap with MaxRAMPercentage, measuring native memory with NMT, and being aware of the GC and processor defaults a fractional vCPU causes, while Actuator probes and graceful shutdown keep deployments clean.
Open with
I see two separate problems, and I would first confirm them from the stopped tasks: the stoppedReason and exit code tell me which one each task hit. The unhealthy ones are a startup problem.
Then, if they want more
On half a vCPU Spring Boot can take close to a minute to start, Fargate does not burst, and the load balancer starts health checking right away, so after a couple of failed checks ECS kills a task that was just slow, and the replacement is equally slow.
I would set healthCheckGracePeriodSeconds on the ECS service and a startPeriod on the container health check above the measured startup time, or give the task a full vCPU. Exit code 137 is 128 plus 9, a SIGKILL, and on ECS that is the memory limit. The JVM uses much more than the heap: metaspace, code cache, thread stacks, direct buffers.
If someone set Xmx close to 1 GB, the total goes over and the kernel kills it without any Java OutOfMemoryError. I would use MaxRAMPercentage around 70 to 75 instead of a fixed Xmx, and check native memory with Native Memory Tracking.
I would also remember that on a fractional vCPU the JVM sees one processor, so it picks Serial GC and sizes pools for one CPU. I can set the GC or ActiveProcessorCount explicitly if that matters.
Finally, health checks should point to Actuator's readiness and liveness endpoints, without the database in liveness, and graceful shutdown should be on so deployments drain in-flight requests.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Spring Boot 3.4
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- AWS documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Spring Boot service on ECS Fargate with 0.5 vCPU and 1 GB memory is killed and...": https://scrivio-interview-library.netlify.app/spring-boot/aws/b9b0194b9b/