Fixing JVM OutOfMemoryError on ECS (EC2 Based) | TO THE NEW Blog
Summary
A Spring Boot service running on ECS (EC2) experienced repeated JVM OutOfMemoryError crashes that destabilized SQS listeners and caused messages to land in DLQs. The team required a single immutable Docker image for QA and Prod and no Jenkins or build-time changes, so they moved environment-specific JVM options into ECS task definition environment variables (JAVA_OPTS) and kept a common ENTRYPOINT. In production they applied percentage-based heap sizing (MaxRAMPercentage/InitialRAMPercentage), set MaxMetaspaceSize, and enabled ExitOnOutOfMemoryError and UseContainerSupport to reserve native memory and ensure clean JVM exit on OOM. They verified JVM sizing inside a running task and reported that OOMs stopped, DLQ growth ceased, the service stabilized, and the QA behavior and CI pipeline remained unchanged.