Created
February 21, 2026 23:41
-
-
Save sam-saffron-jarvis/777cb6fe1df7a630f3764539ee53f801 to your computer and use it in GitHub Desktop.
Show real memory usage (working set) per Docker container, excluding page cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Show real memory usage (working set) per container | |
| # working set = memory.current - inactive_file (excludes reclaimable page cache) | |
| printf "%-30s %10s %10s %10s\n" "CONTAINER" "TOTAL" "CACHE" "WORKING SET" | |
| printf "%-30s %10s %10s %10s\n" "---------" "-----" "-----" "-----------" | |
| docker ps --format '{{.ID}} {{.Names}}' | while read id name; do | |
| cgroup=$(docker inspect --format '{{.Id}}' "$id") | |
| # Try cgroup v2 first, fall back to v1 | |
| mem_current="/sys/fs/cgroup/system.slice/docker-${cgroup}.scope/memory.current" | |
| mem_stat="/sys/fs/cgroup/system.slice/docker-${cgroup}.scope/memory.stat" | |
| if [ ! -f "$mem_current" ]; then | |
| mem_current="/sys/fs/cgroup/memory/docker/${cgroup}/memory.usage_in_bytes" | |
| mem_stat="/sys/fs/cgroup/memory/docker/${cgroup}/memory.stat" | |
| fi | |
| if [ ! -f "$mem_current" ]; then | |
| printf "%-30s %10s\n" "$name" "N/A" | |
| continue | |
| fi | |
| total=$(cat "$mem_current") | |
| inactive_file=$(grep '^inactive_file' "$mem_stat" 2>/dev/null | awk '{print $2}') | |
| inactive_file=${inactive_file:-0} | |
| working_set=$(( total - inactive_file )) | |
| printf "%-30s %10s %10s %10s\n" \ | |
| "$name" \ | |
| "$(numfmt --to=iec $total)" \ | |
| "$(numfmt --to=iec $inactive_file)" \ | |
| "$(numfmt --to=iec $working_set)" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment