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
|
# SketchyBar memory monitoring plugin
|
||||||
|
# Shows actual memory pressure (excludes file cache/inactive pages)
|
||||||
home.file.".config/sketchybar/plugins/memory.sh" = mkIf cfg.sketchybar.enable {
|
home.file.".config/sketchybar/plugins/memory.sh" = mkIf cfg.sketchybar.enable {
|
||||||
executable = true;
|
executable = true;
|
||||||
text = ''
|
text = ''
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
MEMORY_STATS=$(vm_stat)
|
# Use awk for all arithmetic to avoid bash integer overflow on large RAM systems
|
||||||
PAGES_FREE=$(echo "$MEMORY_STATS" | grep "Pages free" | awk '{print $3}' | tr -d '.')
|
# Memory pressure = Anonymous (app memory) + Wired + Compressor RAM
|
||||||
PAGES_ACTIVE=$(echo "$MEMORY_STATS" | grep "Pages active" | awk '{print $3}' | tr -d '.')
|
# - Anonymous pages: app-allocated memory (heap, stack) - matches Activity Monitor's "App Memory"
|
||||||
PAGES_INACTIVE=$(echo "$MEMORY_STATS" | grep "Pages inactive" | awk '{print $3}' | tr -d '.')
|
# - Wired: kernel/system memory that can't be paged out
|
||||||
PAGES_WIRED=$(echo "$MEMORY_STATS" | grep "Pages wired down" | awk '{print $4}' | tr -d '.')
|
# - Pages occupied by compressor: actual RAM used by compressor (NOT "stored in compressor")
|
||||||
PAGES_COMPRESSED=$(echo "$MEMORY_STATS" | grep "Pages stored in compressor" | awk '{print $5}' | tr -d '.')
|
TOTAL_RAM=$(sysctl -n hw.memsize)
|
||||||
|
MEMORY_PERCENT=$(vm_stat | awk -v total_ram="$TOTAL_RAM" '
|
||||||
TOTAL_PAGES=$((PAGES_FREE + PAGES_ACTIVE + PAGES_INACTIVE + PAGES_WIRED + PAGES_COMPRESSED))
|
/page size of/ { page_size = $8 }
|
||||||
USED_PAGES=$((PAGES_ACTIVE + PAGES_INACTIVE + PAGES_WIRED + PAGES_COMPRESSED))
|
/Anonymous pages/ { anon = $3 + 0 }
|
||||||
MEMORY_PERCENT=$((USED_PAGES * 100 / TOTAL_PAGES))
|
/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%"
|
${pkgs.sketchybar}/bin/sketchybar --set $NAME label="$MEMORY_PERCENT%"
|
||||||
'';
|
'';
|
||||||
|
|||||||
Reference in New Issue
Block a user