Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Docker

Docker: Fix a Container That Gets OOM-Killed Even Though the Host Has Memory Free

- 06.09.26 - ErcanOPAK

🐳 Your Container Keeps Getting OOM-Killed – While the Host Sits There With Memory to Spare

A container dies with an out-of-memory error, and `free -h` on the host shows plenty of memory available – which seems like a contradiction, until you realize Docker enforces a MEMORY LIMIT scoped to the container itself (via cgroups), entirely independent of what the host machine has free; the container is killed for exceeding ITS OWN ceiling, not the host’s.

🔎 The Problem

$ docker run -m 512m my-app

# The container is capped at 512MB regardless of how much RAM the host
# actually has - if the application inside genuinely needs more than
# 512MB under real load (a memory-hungry request, a growing in-memory
# cache), the kernel'"'"'s OOM killer terminates the container'"'"'s process
# the moment it crosses that limit - "Exited (137)" - even while
# `free -h` on the host itself shows tens of gigabytes still free.

✅ Fix: Diagnose the Actual Memory Need, Then Set the Limit to Match

  • `docker stats` while the app is under realistic load shows the container’s actual memory usage over time – if it’s consistently approaching the limit before being killed, that’s confirmation the limit itself (not a leak) is the immediate cause, and it needs to be raised to match real usage.
  • If usage keeps climbing without ever leveling off, that’s a genuine memory leak inside the application, and raising the limit only delays the same crash – profile the app’s memory usage directly (heap dumps, a memory profiler for the runtime in use) rather than treating a higher limit as the fix.
  • For JVM, Node, and similar managed runtimes specifically, make sure the RUNTIME’s own memory settings (`-Xmx` for Java, `–max-old-space-size` for Node) are set BELOW the container’s memory limit – a runtime that doesn’t know about the container ceiling can allocate right up to what it thinks the whole host has, only to get killed by Docker before the runtime’s own garbage collector even reacts.

⚠️ Reading Exit Code 137

  • Exit code 137 specifically means the container process received SIGKILL (128 + signal 9) – it’s the reliable signature of an OOM kill (or a manual `docker kill`), distinct from an application crash that would show a different, application-specific exit code.

A container’s memory limit isn’t a suggestion tied to the host’s free memory – it’s a hard ceiling the kernel enforces regardless, and ‘the host has plenty free’ was never the number that mattered.

— DevOps Engineer

Related posts:

Docker: Fix 'No Space Left on Device' Without Deleting Your Actual Data

Docker: Master Container Networking

Docker: Production Logging with ELK Stack

Post Views: 1

Post navigation

JavaScript: Why Comparing Two Dates With == Almost Never Works the Way You Expect
C#: Fix an IDisposable Object That Never Gets Disposed Because of an Early Return

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

October 2026
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Sep    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (973)
  • How to make theater mode the default for Youtube (951)
  • How to add default value for Entity Framework migrations for DateTime and Bool (939)
  • Get the First and Last Word from a String or Sentence in SQL (847)
  • How to select distinct rows in a datatable in C# (837)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (624)
  • Add Constraint to SQL Table to ensure email contains @ (590)
  • Average of all values in a column that are not zero in SQL (553)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (526)
  • Find numbers with more than two decimal places in SQL (468)

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps
  • Photoshop: Fix a Color Profile Mismatch That Makes Printed Output Look Nothing Like What You Saw On Screen
  • WordPress: Fix Search Results That Return Pages From a Theme You Deactivated Months Ago
  • Visual Studio: Fix a Test Project That Builds Fine Alone but Fails to Discover Any Tests After a NuGet Restore
  • ASP.NET Core: Fix a File Upload That Times Out on Slow Connections Only Because Kestrel’s Minimum Data Rate Feature Kicked In
  • JavaScript: Fix an Array Destructuring Default Value That Silently Never Applies Because null Was Passed Instead of Undefined

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (973)
  • How to make theater mode the default for Youtube (951)
  • How to add default value for Entity Framework migrations for DateTime and Bool (939)
  • Get the First and Last Word from a String or Sentence in SQL (847)
  • How to select distinct rows in a datatable in C# (837)

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com