Fix sketchybar memory indicator to show actual memory pressure
The previous implementation included inactive/cached pages and used "Pages stored in compressor" (uncompressed size), resulting in inflated percentages (~88%) that didn't reflect actual memory pressure. Now uses: - Anonymous pages (matches Activity Monitor's "App Memory") - Pages wired down (system memory) - Pages occupied by compressor (actual RAM used, not uncompressed size) Also switches to awk for arithmetic to avoid bash integer overflow on systems with >4GB RAM.
This commit is contained in:
@@ -599,21 +599,28 @@ in
|
||||
};
|
||||
|
||||
# SketchyBar memory monitoring plugin
|
||||
# Shows actual memory pressure (excludes file cache/inactive pages)
|
||||
home.file.".config/sketchybar/plugins/memory.sh" = mkIf cfg.sketchybar.enable {
|
||||
executable = true;
|
||||
text = ''
|
||||
#!/bin/bash
|
||||
|
||||
MEMORY_STATS=$(vm_stat)
|
||||
PAGES_FREE=$(echo "$MEMORY_STATS" | grep "Pages free" | awk '{print $3}' | tr -d '.')
|
||||
PAGES_ACTIVE=$(echo "$MEMORY_STATS" | grep "Pages active" | awk '{print $3}' | tr -d '.')
|
||||
PAGES_INACTIVE=$(echo "$MEMORY_STATS" | grep "Pages inactive" | awk '{print $3}' | tr -d '.')
|
||||
PAGES_WIRED=$(echo "$MEMORY_STATS" | grep "Pages wired down" | awk '{print $4}' | tr -d '.')
|
||||
PAGES_COMPRESSED=$(echo "$MEMORY_STATS" | grep "Pages stored in compressor" | awk '{print $5}' | tr -d '.')
|
||||
|
||||
TOTAL_PAGES=$((PAGES_FREE + PAGES_ACTIVE + PAGES_INACTIVE + PAGES_WIRED + PAGES_COMPRESSED))
|
||||
USED_PAGES=$((PAGES_ACTIVE + PAGES_INACTIVE + PAGES_WIRED + PAGES_COMPRESSED))
|
||||
MEMORY_PERCENT=$((USED_PAGES * 100 / TOTAL_PAGES))
|
||||
# Use awk for all arithmetic to avoid bash integer overflow on large RAM systems
|
||||
# Memory pressure = Anonymous (app memory) + Wired + Compressor RAM
|
||||
# - Anonymous pages: app-allocated memory (heap, stack) - matches Activity Monitor's "App Memory"
|
||||
# - Wired: kernel/system memory that can't be paged out
|
||||
# - Pages occupied by compressor: actual RAM used by compressor (NOT "stored in compressor")
|
||||
TOTAL_RAM=$(sysctl -n hw.memsize)
|
||||
MEMORY_PERCENT=$(vm_stat | awk -v total_ram="$TOTAL_RAM" '
|
||||
/page size of/ { page_size = $8 }
|
||||
/Anonymous pages/ { anon = $3 + 0 }
|
||||
/Pages wired/ { wired = $4 + 0 }
|
||||
/Pages occupied by compressor/ { compressor = $5 + 0 }
|
||||
END {
|
||||
used = (anon + wired + compressor) * page_size
|
||||
printf "%.0f", used / total_ram * 100
|
||||
}
|
||||
')
|
||||
|
||||
${pkgs.sketchybar}/bin/sketchybar --set $NAME label="$MEMORY_PERCENT%"
|
||||
'';
|
||||
|
||||
Reference in New Issue
Block a user