Skip to content

Free background browser tabs when the computer runs low on memory - #52667

Merged
brennanb2025 merged 9 commits into
mainfrom
brennanb2025/urgent-browser-discard
Nov 30, 2026
Merged

brennanb2025 merged 9 commits into
mainfrom
brennanb2025/urgent-browser-discard

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

When your computer is about to run out of memory, Orca now closes background browser tabs you haven't looked at in a while, oldest first, so the computer stays usable. When you go back to one, it reloads where you left it. Before this, Orca waited 4 hours no matter how little memory was left.

Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​168 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​168
Prod 9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​252 $\color{#cf222e}{\Huge{\mathbf{−}}}$​82 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​170

What Changed

  • Before: a hidden browser page was freed only after Memory Saver's hours (4 by default) of awake time unviewed. If memory ran out sooner, every hidden page stayed loaded. Each one is its own browser process.
  • After: when the machine's available memory is critically low, Orca frees the hidden page that was seen least recently, if it has been hidden at least 10 minutes. It frees at most one page every 10 seconds while memory stays that low.
  • A page that is protected when memory gets low, for example one playing audio, is skipped for the rest of the time it stays hidden. It still gets its normal Memory Saver free after the usual hours, if nothing protects it by then.
  • Just before a page is freed, Orca checks memory again. If memory has recovered by then, the page stays loaded until its normal time.
  • Freeing uses the same path and the same protections as Memory Saver. A page is never freed while it's visible, used by an agent, has unsaved edits, plays or recently played audio, is capturing, has developer tools or a popup open, is downloading, can send notifications, or is visited often. Turning Memory Saver off turns this off too.
  • "Critically low" means the machine's available physical memory is at most 400 MiB on Windows and Linux, or at most 800 MiB on macOS, where the count includes the file cache. Below 1,000 MiB (1,200 MiB on macOS) counts as moderately low.
  • How memory is checked (system-memory-pressure.ts): Orca reads Electron's process.getSystemMemoryInfo(), a synchronous system call that starts no other process.
    • Orca already samples system memory every 10 seconds for crash reports. The urgent check runs right after each of those samples, through a small listener hub (host-memory-sample-tick.ts), so it adds no timer and no wakeup.
    • The check reads memory only when a page is a candidate. It reads the awake clock only once memory is critical, so it doesn't keep the clock's heartbeat running while memory is healthy.
    • Nothing is checked when no page is hidden, or when Memory Saver is off.
  • Each free request now carries its own attempt number, so a late or duplicate refusal of one request can't cancel a newer one for the same page. Main and the renderer both send a refusal when main says no. The renderer passes the request's token back unchanged, so the renderer needs no change.
  • A new candidate list (browser-page-urgent-discard.ts) keeps hidden pages in the order they were hidden, so finding the next page to free never scans every page. browser-page-idle-episode.ts holds the page record and the "hidden and waiting" rule; the scheduler and the hidden-page count used to repeat that rule.
  • The settings sync moves to its own file (browser-memory-saver-settings-sync.ts) so the scheduler file stays under the line limit. Its code is unchanged.

Why

On a fleet copy at 1,901 workspaces, 30 hidden browser pages took about 1.65 GB, two thirds of the app's memory. Users have reported the same pattern (20 background renderers taking 1.66 GB), and the crash channel shows renderer out-of-memory crashes on Windows. A timer measured in hours can't help when memory runs out within minutes.

This is the standard approach: free the least recently seen background page under critical system memory pressure, keep a short grace period for pages seen recently, and repeat at a fixed pace while the pressure lasts. Two alternatives were rejected:

  • A shorter default Memory Saver timer frees pages even when memory is plentiful.
  • A cap on the number of loaded tabs frees pages for no reason on large machines.

Linked Issue

Fixes #

Visual Proof

N/A: no UI change. A freed page shows its normal reload when opened again, as Memory Saver pages already do.

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below
  • Every added test fails without this change and avoids the test anti-patterns

Measured on Linux on head a6c8597, against main, with the same copy of a synthetic 1,901-workspace profile for both builds. The copy had 32 browser pages (16 opened the way a person would, 16 through the runtime), 30 chats and 40 terminals with 20,000 lines each, plus 126 workspace switches. The app's own memory reading was set to critically low for 14 minutes while the copy saw light use, one workspace switch a minute. Memory is PSS for all of Orca's processes:

main this PR
Total app memory 1,938 MB 493 MB
Browser page processes 32 (1,325 MB) 0
App window renderer 280 MB 261 MB
  • Reopening a freed page: it reloaded in 654 ms with the same address, title and content. Its scroll position came back: it was scrolled to y=4321 before being freed. A page that was never freed shows in about 531 ms.

  • Freeing pace: one page every 10 seconds (measured gaps of 9,999 to 10,004 ms), oldest first. All 32 went through the normal admission.

  • Without simulated pressure: nothing is freed before Memory Saver's hours, as before.

  • browser-page-idle-discard.test.ts covers the state machine:

    • Pressure frees only the oldest page that has been hidden 10 minutes, and frees nothing once pressure ends.
    • A page that refused an urgent free still gets its normal 4-hour free.
    • A late duplicate refusal of an urgent free doesn't cancel the normal free sent after it.
    • A request whose pressure cleared before admission is refused.
    • With 300 hidden pages, urgent freeing owns no timer. It reads memory once per shared sample, and nothing is freed until memory is critical.
  • Every test fails on main.

  • Tested on Linux only. The Windows and macOS thresholds use the fields Electron reports on those platforms and haven't been run there.

  • pnpm check:changed passes.

Performance

  • No new timer or wakeup: the check runs on the existing 10-second crash-report memory sample.
  • Per sample: one synchronous system call, and only when a hidden page is a candidate.
  • Bounded work: finding the next page to free reads the oldest entry of a sorted list, so the cost doesn't grow with the number of pages. Only one urgent free request is in flight at a time, and each sample frees at most one page.

Risk

Level: Low

  • Under real memory pressure, pages hidden 10 minutes or more can be freed long before the 4-hour mark. Reopening such a page reloads it, as Memory Saver pages already do.
  • Every protection that keeps a page loaded still applies.
  • Back out with a plain revert.

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

  • Cross-platform: each platform's threshold uses the field that platform reports for available memory. An unknown platform, or a reading that fails, never counts as pressure.
  • SSH/remote: browser pages run on the desktop, so the reading is the desktop's own memory.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Performance review (perf-scan) on a6c8597

Verdict: PASS. I found no new timers, no new wakeups at idle, and no work that grows with the number of pages per tick.

I checked git diff origin/main...HEAD at a6c8597 against the code. The new tests pass (5/5). I didn't profile the running app, so there are no timing numbers here, only counts.

Main finding: the 10 s memory sample isn't new. startPreGoneCrashSampling() already starts it unconditionally at startup (main-process-ready-runtime.ts:50 → pre-gone-host-memory.ts:102). The diff only adds a listener to that tick through the new hub (notifyHostMemorySample(), pre-gone-host-memory.ts:98). Idle wakeups per second on main, renderer and daemon are the same as on main.

New or changed work, with trigger, population and bound

Work Trigger Per event Bound
notifyHostMemorySample (hub) existing 10 s sample, only after a successful sample runs 1 listener, each wrapped in try/catch listener Set with an unsubscribe; one subscriber (the scheduler singleton)
urgent.sample the same tick peeks the heap (O(1)), then the ready check (setting on, power state, nothing in flight), then one memory read, then one awake-clock read only if memory is critical frees at most 1 page per tick; blocked while a request is in flight; a page that refused stays out until it is hidden again
Urgent candidate heap updates visibility reports, retire, remove, unknown visibility (unindex) 2 heap operations instead of 1, O(log n) entries are removed on every path that removes the old deadline-heap entry
admit check for urgent requests one renderer acknowledgment per sent request 1 memory read, only when urgent is set and the page has been hidden ≥10 min 1 per request
attempt field on the token each request integer compare none needed
Settings sync moved to browser-memory-saver-settings-sync.ts settings change unchanged (moved as-is) the earlier listener is stopped first

Scenarios

  • Idle, 0 hidden pages: each tick does one peek of an empty heap. No memory read, no clock read, nothing new.
  • 30 or 300 hidden pages, memory healthy: each tick does one peek, the ready check and one memory read. That is 1 read per 10 s whatever the page count. The test at line 108 confirms 300 pages give 6 reads over 6 ticks and the same timer count. The awake clock isn't read, so its 20 s heartbeat still stops after 15 minutes without a read.
  • 30 or 300 hidden pages, memory critical: one clock read and at most one freed page per tick. 300 eligible pages take about 50 minutes to drain, which follows Chromium's one-at-a-time behaviour. Reading the clock keeps its heartbeat (one 20 s timer) running, but only while memory is critical and a candidate exists.
  • After sleep/wake: while suspended, the ready check fails before memory is read. The awake clock doesn't count sleep, so the 10-minute hidden requirement doesn't fire for everything on wake. No catch-up burst, because it's still one page per tick.
  • Memory Saver off: the ready check returns early before any memory or clock read. Candidates are still tracked in the heap, the same way the existing deadline heap already does.
  • Dispose: unsubscribeSamples() removes the listener from the hub and urgent.dispose() clears the heap. The rest of the teardown is unchanged.
  • Stuck acknowledgment: if a request is never answered, urgent freeing simply stops. That costs no work, but no further urgent frees happen until the request is cleared. This was already true of the in-flight tracking before the change.

Six scan questions

  1. Work before the gate: reviewed. Memory is read only when there's a candidate and the ready check passes; the clock only when memory is critical.
  2. Work multiplied by owners: reviewed. There's one listener on a singleton.
  3. Thread and storage: reviewed. Each memory read is a synchronous call on Electron main, at most one every 10 s. No SQLite or filesystem access.
  4. Growth and progress: reviewed. O(log n) per visibility event, O(1) per tick, and each tick either frees a page or exits.
  5. Execution host: reviewed. Thresholds are set per platform; an unknown platform or a failed read counts as no pressure.
  6. Teardown: reviewed. Covered above under dispose and stuck acknowledgment.

Regression classes the diff touches

  • Fixed-cadence poll, new idle wakeups: matched and disproved. There's no new timer; it reuses the existing 10 s interval.
  • Hidden owners keep working: matched and disproved. Work per tick is O(1) and doesn't depend on how many pages are hidden.
  • Per-entity state outlives the entity: matched and disproved. Every removal path that clears the deadline heap also clears the candidate heap, via unindex.
  • Sync calls on Electron main: matched and disproved. One memory read per 10 s at most.

Nit, not a finding: readSystemMemoryLevel calls the OS memory query again on the same tick where commitHostMemorySample already did. The sample could pass its reading to the listener instead. It's one extra cheap call per 10 s, and only when a candidate exists.

Not checked: how the renderer handles the extra urgent free requests (their rate is capped at one per tick), Windows and Linux on real machines, and live CPU numbers.

@brennanb2025
brennanb2025 marked this pull request as ready for review November 29, 2026 23:48
@greptile-apps

greptile-apps Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5 · The PR appears safe to merge, with no outstanding findings.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with no outstanding findings.

Summary

Adds memory-triggered freeing of background browser pages through the existing Memory Saver protections.

  • Orca frees eligible hidden browser pages when memory is critically low.
  • Memory Saver settings syncing now sits outside the page scheduler.

Reviews (4) · Last reviewed commit: "test(memory): cover each platform's avai..." · Reviewed by Greptile

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Review status for a6c8597

Problem: hidden browser pages each keep a renderer process, about 55 MB apiece. On a 1,901-workspace profile with 30 of them, they were two thirds of the app's memory. Memory Saver freed them only after 4 awake hours, even when the computer was running out of memory.

Change: when available memory is critically low (at most 400 MiB on Windows and Linux, 800 MiB on macOS including file cache), Orca frees the least recently seen hidden page that has been hidden at least 10 minutes. It frees at most one page per 10-second memory sample, through Memory Saver's existing path and protections. Turning Memory Saver off turns this off too.

Verified:

  • Benchmark: head=a6c859716c05490123688e1b4462cacd62982da2 gate=PASS corpus=synthetic 1,901-workspace fleet copy (32 browser pages, 30 chats, 40 terminals with 20k lines), Linux PSS, critical memory reading simulated for 14 min. Total app memory: main 1,938 MB, this PR 493 MB. Browser page processes: 32 (1,325 MB) to 0. App window renderer: 280 to 261 MB. Listeners mounted: Memory Saver scheduler only.
  • A freed page reopened in 654 ms (a live page takes about 531 ms) with the same address, title and content, and its scroll position restored.
  • Pages were freed 10 s apart, oldest first, and all went through normal admission.
  • Unit tests cover the scheduler state machine: every test fails on main.
  • pnpm check:changed passes.
  • Tested on Linux only. Windows and macOS read the platform fields Electron reports but weren't run.

Fixed during review:

  • A refused urgent free used to cancel the page's normal 4-hour free.
  • An idle wakeup: there is now no timer of its own; the check rides the existing crash-report memory sample.
  • Visibility changes could postpone a due check.
  • Admission now re-checks pressure.
  • A late duplicate refusal could settle a newer request. Each request now carries its own attempt number.

Deferred (P2 and nits):

  • No per-platform table test for the memory reading.
  • A request that never gets an answer blocks urgent freeing, the same way it already blocks Memory Saver.
  • Each protected page costs one 10 s sample under pressure.
  • The "moderate" level is computed but unused.
  • System memory is read twice per sample.

Final passes on this head: regression audit SAFE, perf-scan PASS, Claude re-review CLEAN.

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds platform-specific system memory classification and host-memory sample notifications. Browser page discard now checks eligible hidden pages during shared memory samples and can issue an urgent request under critical pressure. Requests carry an attempt token, and urgent admission rechecks memory pressure. The change also moves settings synchronization to a separate module and adds tests for urgent and normal discard behavior.

Merge Risk: Minimal · up to fb461

This change frees long-hidden browser tabs when available memory is critically low, which reduces app memory use. No concrete merge-blocking risk was found. The author's testing was on Linux only, so Windows and macOS thresholds have not been exercised on those platforms.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "Free background browser tabs when the computer runs low on memory" matches the main change: the PR adds urgent freeing of hidden browser pages when system memory is critically low. It is sp…
Description check Passed The description follows the repository template and covers all required sections. It has an ELI5, a before/after account of What Changed, Why with alternatives considered, Testing with Linux-only manu…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Free background browser tabs when the computer runs low on memory" matches the main change: the PR adds urgent freeing of hidden browser pages when system memory is critically low. It is specific and does not use vague terms. Minor nuance: the change frees hidden browser pages (Memory Saver), so "tabs" is a reasonable user-facing term.

Full details: Description check

Explanation

The description follows the repository template and covers all required sections. It has an ELI5, a before/after account of What Changed, Why with alternatives considered, Testing with Linux-only manual testing and tests, Performance with a specific justification, and Risk with level and revert method. It has a Visual Proof section marked N/A with a reason. The Linked Issue section is incomplete: 'Fixes #' has no issue number, and the template says outside contributors must link an actual issue. The AI Disclosure section is empty. Some template comments and the PR LoC block are left in place. The Notes and Checklist are filled in. The main gaps are the missing linked issue and the empty AI Disclosure section.

Resolution

Fill in the issue number after "Fixes #" (or confirm that the author is a maintainer and no issue is needed). Complete the AI Disclosure section, or state that it does not apply to this author. Consider stating the Windows and macOS test status in the Testing section, which it already says was not run.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 17ce288e-1ee0-4589-97d8-123d92fb8456
Commits

Reviewing files that changed from the base of the PR and between 15c41d7 and fb46112.

Files selected for processing: 1

  • src/main/memory/system-memory-pressure.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

@brennanb2025
brennanb2025 merged commit c0805b8 into main Nov 30, 2026
58 checks passed
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