Answer to the exact question
Memory thresholds that trigger instability:
- WebContainers start with a 16 MiB WebAssembly linear memory and can grow up to the browser’s maximum (often 1 GiB).
- On a low‑spec machine (≈4 GiB RAM, 2‑core CPU) instability—manifested as OOM exceptions or a crashing tab—typically occurs when the runtime’s RSS reaches ~700 MiB.
- On higher‑spec devices the threshold can be 1.2–1.5 GiB, but the exact value is browser‑dependent and may shift with updates.
How large npm installs are handled:
- Packages are streamed into an IndexedDB‑backed virtual file system. The runtime loads modules on demand and keeps a cache of recent modules.
- When memory pressure is detected, the runtime may evict unused modules from the cache or refuse to load more, resulting in install failures or increased download time.
- If the total size of the package tree exceeds the per‑origin IndexedDB quota (≈50 MiB in many browsers), the install fails before the WebAssembly memory is even allocated.
- There is no API to set a hard cap on WebAssembly memory; attempts to override the default are ignored by the runtime.
Explanation and Practical Steps
1. Verify COOP/COEP headers – Open DevTools, inspect the request that loads the WebContainer entry point, and ensure the response contains:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
If missing, SharedArrayBuffer support is disabled and the memory cap drops.
2. Monitor memory during a test install – Inside the container run:
setInterval(() => {
const { rss, heapUsed } = process.memoryUsage();
console.log(`RSS: ${rss} B, Heap: ${heapUsed} B`);
}, 1000);
Check the values in the console while installing a large package (e.g.,
npm install react webpack).
3. Check IndexedDB quota – In DevTools, navigate to Application → Storage → IndexedDB, and examine the size of the WebContainer database. If it is approaching the browser’s per‑origin quota, consider splitting the install or using a CDN‑cached .tgz that the runtime can fetch lazily.
4. Use --no-optional or --no-audit flags to reduce the amount of metadata downloaded, which can lower the memory footprint during installation.
5. Test on representative devices – Run the same install on a low‑spec laptop and a high‑spec desktop to capture the threshold range. Record the RSS at which the runtime throws an OOM exception or the tab becomes unresponsive.
Follow‑up Diagnostic Detail
To fine‑tune the recommendation, could you share the typical device class of your target users (e.g., minimum RAM, CPU cores, browser version)? This will help refine the memory threshold estimate and suggest whether a lighter runtime configuration is needed.