跪拜 Guibai
← All articles
Electron

Slash Electron Idle Memory from 500MB to 163MB with Lazy WebView Mounting and esbuild Minification

By GogoStudio ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Hidden webview renderers can quietly consume 100–250 MB each in multi-tab Electron apps, and this recipe gives Western developers a proven pattern to reclaim that memory without regressing tab-switch feel. It also shows why standard esbuild minification beats heavy obfuscation for Electron main processes, especially when aiming at developer-facing tools.

Summary

Multi-webview Electron apps often hide the real memory leak in `<webview>` renderer processes that sit behind `display: none` long after the user stopped looking at them. This article documents three concrete cuts applied to a local AI workbench that brought idle total memory from 480–550 MB down to 163.3 MB and shrank the main-process bundle from 483 KB to 295.7 KB.

The first cut is the big one: delay webview mounting by one frame using `requestAnimationFrame` so tab switching stays visually instant, then automatically unmount hidden webviews after 10 minutes of inactivity so Chromium actually reaps their renderer processes. A "low memory mode" toggle unmounts immediately on tab switch for memory-constrained machines. The second cut replaces the Electron build with `esbuild --minify --legal-comments=none` and explicitly disables sourcemaps, which alone reduced `main.cjs` by 39% while deliberately skipping heavy obfuscation shells that slow startup and trigger Windows antivirus false positives.

The third cut adds a settings-panel memory monitor that aggregates per-process `workingSetSize` via `app.getAppMetrics()`, plus a one-click cache clear that uses `clearCache()` rather than `clearStorageData()` so user login cookies and localStorage survive. Together these changes keep a four-process Electron app at 163 MB idle without losing responsiveness.

Takeaways
— Idle memory dropped from 480–550 MB to 163.3 MB after three optimizations on Electron 35.7.5 (macOS Apple Silicon).
— Main process `main.cjs` shrank from 483 KB to 295.7 KB using `esbuild --minify --legal-comments=none` and `sourcemap: false`.
— Delaying webview mounting by one frame with `requestAnimationFrame` eliminates click-to-highlight jank while still allowing lazy load.
— Hidden webviews are automatically unmounted after 10 minutes of inactivity, letting Chromium reclaim their renderer processes; a low-memory mode unmounts immediately on tab switch.
— Skipping heavy obfuscation (like `javascript-obfuscator` or V8 bytecode) avoids Windows antivirus false positives and slower startup; standard esbuild minification already cuts size by 39%.
— Use `session.clearCache()` instead of `clearStorageData()` to clear cached images/scripts without wiping user login cookies or localStorage.
— `app.getAppMetrics()` can aggregate per-process `workingSetSize` into a real-time memory diagnostic card in the settings page.
Conclusions

The biggest memory win comes from treating hidden webviews as a lifecycle-managed resource rather than relying on Chromium's own behavior; a 10-minute inactivity timer is a pragmatic middle ground between immediate destruction and permanent residency.

Delaying mounting by one frame is a clever trick to keep tab switching visually responsive while still deferring the expensive webview creation until after the UI update paints.

For developer-oriented Electron tools, transparency wins over obfuscation: users will unpack app.asar anyway, so standard minification builds trust and avoids antivirus flagging.

Using `clearCache()` over `clearStorageData()` is a subtle but crucial distinction: it preserves authentication state while still reclaiming disk and memory from cached assets.

Concepts & terms
mountedWebTabs
A React state set that tracks which webview tabs are currently mounted in the DOM. Tabs not in this set are unmounted, so Chromium can reclaim their renderer process memory.
workingSetSize
A metric from Chromium/Electron's `app.getAppMetrics()` that reports how much memory each process (browser, tab, GPU, utility) is currently using. Aggregating it gives a real-time total memory snapshot.
clearCache() vs clearStorageData()
In Electron, `session.clearCache()` removes cached HTTP resources (images, scripts) while leaving storage like cookies and localStorage intact. `clearStorageData()` clears all storage, which would log users out.
From the discussion

The discussion weighs a framework swap against incremental Electron tuning. One position says cutting memory bloat outright by moving to Tauri. Another engages with the article's specifics, judging the 10-minute recycle window a pragmatic compromise and calling out the clearStorageData avoidance as essential to protecting login state. This shows a split between a clean break and optimizing what Electron already offers.

— Switching to Tauri eliminates the Electron memory problem entirely rather than patching it.
— A 10-minute recycle window balances annoyance from frequent remounts against idle memory waste.
— Bypassing clearStorageData when clearing cache is critical so login state survives.
— The tension is whether to accept Electron's overhead and fine-tune it or discard it for a lighter framework.
Featured comments
去吧波波鸟

The 10-minute figure is interesting. Too short and it gets annoying switching back and forth, too long and it just sits there wasting memory. I'll note that the cache clearing deliberately avoids clearStorageData, because getting logged out is a real pain.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗