Skip to content

Fix macOS memory leak in app info provider (instead of restarting espanso daily) - #2632

Merged
n8henrie merged 2 commits into
espanso:devfrom
dmgerman:dev
Mar 7, 2026
Merged

n8henrie merged 2 commits into
espanso:devfrom
dmgerman:dev

Conversation

@dmgerman

@dmgerman dmgerman commented Feb 1, 2026

Copy link
Copy Markdown

The espanso worker process was consuming ~1.8 GB of memory despite having only
~45 simple text triggers configured. The process had been running for 10 days.

Memory usage observed:

  • Worker process: 1,905,760 KB (~1.8 GB)
  • Launcher: 77,024 KB (~75 MB)
  • Daemon: 23,376 KB (~23 MB)

The Objective-C functions that query macOS for active application information
were missing @autoreleasepool blocks.

When methods like [NSRunningApplication runningApplicationWithProcessIdentifier:]
or [[NSWorkspace sharedWorkspace] frontmostApplication] are called, they
return autoreleased objects. In Cocoa, autoreleased objects are added to an
autorelease pool and freed when that pool drains.

However, these functions are called from Rust threads that don't have an
autorelease pool. Without a pool, the autoreleased objects accumulate
indefinitely until the thread exits.

It is now down to approximately 20-30 MBytes of usage.

@dmgerman
dmgerman requested a review from Archigos as a code owner February 1, 2026 19:40
@dmgerman dmgerman mentioned this pull request Feb 2, 2026
@n8henrie

n8henrie commented Mar 4, 2026

Copy link
Copy Markdown
Collaborator

For some reason CI didn't run tests for macos ¯\_(ツ)_/¯

Fixed in d56f154

Can you rebase on the latest dev? I imagine some clippy lints will fail, but hopefully the macos tests will pass.

dmgerman added 2 commits March 4, 2026 22:32
Add @autoreleasepool blocks to Objective-C functions in
espanso-info/src/cocoa/native.mm that query active application info.

Without autorelease pools, NSRunningApplication and related objects
created by methods like [NSWorkspace frontmostApplication] were never
released when called from Rust threads, causing memory to grow
unboundedly (~1.8GB over 10 days with default 400ms cache interval).

Changes:
- Wrap info_get_title() in @autoreleasepool
- Wrap info_get_title_fallback() in @autoreleasepool
- Wrap info_get_exec() in @autoreleasepool
- Wrap info_get_class() in @autoreleasepool
- Fix info_get_title() to return result instead of hardcoded
The previous commit (0abd1c6) fixed memory leaks in espanso-info by
adding @autoreleasepool blocks. This commit applies the same fix to
all other Objective-C files in the codebase that were missing
autorelease pool protection.

Without autorelease pools, autoreleased Cocoa objects created from
Rust threads, GCD blocks, and event handler callbacks accumulate
indefinitely and are never freed.
@dmgerman

dmgerman commented Mar 5, 2026

Copy link
Copy Markdown
Author

For some reason CI didn't run tests for macos ¯_(ツ)_/¯

Fixed in d56f154

Can you rebase on the latest dev? I imagine some clippy lints will fail, but hopefully the macos tests will pass.

done

@n8henrie
n8henrie merged commit a511538 into espanso:dev Mar 7, 2026
0 of 4 checks passed
@n8henrie

n8henrie commented Mar 7, 2026

Copy link
Copy Markdown
Collaborator

Many thanks @dmgerman!

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.

2 participants