Skip to content

fix: prevent unbounded memory growth in parse_powermetrics#93

Open
zubvit wants to merge 1 commit into
tlkh:mainfrom
zubvit:fix/parse-powermetrics-memory-leak
Open

fix: prevent unbounded memory growth in parse_powermetrics#93
zubvit wants to merge 1 commit into
tlkh:mainfrom
zubvit:fix/parse-powermetrics-memory-leak

Conversation

@zubvit
Copy link
Copy Markdown

@zubvit zubvit commented May 18, 2026

Problem

powermetrics appends a null-byte-separated plist entry to its output file on every interval tick and never truncates it. parse_powermetrics called fp.read() on the entire file every loop iteration — after an hour at the default 1-second interval, it loads thousands of stale samples into RAM on every tick, with memory growing linearly with uptime.

Fix

Seek to 64 KB before EOF and read only the tail. A single plist sample is 4–16 KB, so data[-1] still gets the latest complete entry while memory usage becomes O(1) regardless of how long asitop runs.

-        with open(path+timecode, 'rb') as fp:
-            data = fp.read()
+        with open(path+timecode, 'rb') as fp:
+            fp.seek(0, 2)
+            size = fp.tell()
+            fp.seek(max(0, size - 65536))
+            data = fp.read()

Tested on macOS with Apple M1, asitop 0.0.24.

powermetrics appends a null-byte-separated plist entry to its output
file on every interval tick and never truncates it. parse_powermetrics
was reading the entire file on every loop iteration, so memory grew
linearly with uptime (thousands of stale samples after an hour).

Fix: seek to 64 KB before EOF and read only the tail. A single plist
sample is 4-16 KB so data[-1] still gets the latest complete entry,
but memory usage is now O(1) regardless of how long asitop runs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant