fix(engine): use totalmem() instead of freemem() for memory scaling

Addresses review feedback: freemem() is misleading on macOS where
aggressive file caching reports low free memory even on high-spec
machines. Switched to totalmem()-based thresholds consistent with
calculateOptimalWorkers in parallelCoordinator.ts.

Thresholds now based on total RAM:
- <4GB total: GPU=512MB, V8=256MB, cache=32/128MB
- <8GB total: GPU=1024MB, V8=512MB, cache=64/256MB
- >=8GB total: unchanged (4096MB GPU, no V8 cap, 256/1500MB cache)
This commit is contained in:
Miguel Ángel
2026-05-25 15:37:13 -04:00
parent 2015d93d2a
commit c8ed90fa21
2 changed files with 18 additions and 19 deletions
+9 -10
View File
@@ -208,26 +208,25 @@ export const DEFAULT_CONFIG: EngineConfig = {
debug: false,
};
function getSystemFreeMb(): number {
function getSystemTotalMb(): number {
try {
const { freemem } = require("os") as typeof import("os");
return Math.floor(freemem() / (1024 * 1024));
return Math.floor(require("os").totalmem() / (1024 * 1024));
} catch {
return 8192;
return 16384;
}
}
function memoryAdaptiveCacheLimit(): number {
const free = getSystemFreeMb();
if (free < 2048) return 32;
if (free < 4096) return 64;
const total = getSystemTotalMb();
if (total < 4096) return 32;
if (total < 8192) return 64;
return DEFAULT_CONFIG.frameDataUriCacheLimit;
}
function memoryAdaptiveCacheBytesMb(): number {
const free = getSystemFreeMb();
if (free < 2048) return 128;
if (free < 4096) return 256;
const total = getSystemTotalMb();
if (total < 4096) return 128;
if (total < 8192) return 256;
return DEFAULT_CONFIG.frameDataUriCacheBytesLimitMb;
}
@@ -8,7 +8,7 @@
import type { Browser, PuppeteerNode } from "puppeteer-core";
import { existsSync, readdirSync } from "fs";
import { join } from "path";
import { freemem, homedir } from "os";
import { homedir, totalmem } from "os";
import { DEFAULT_CONFIG, type EngineConfig } from "../config.js";
let _puppeteer: PuppeteerNode | undefined;
@@ -476,21 +476,21 @@ export function _setPuppeteerForTests(mock: PuppeteerNode | undefined): void {
_puppeteer = mock;
}
function getFreeMb(): number {
return Math.floor(freemem() / (1024 * 1024));
function getTotalMemMb(): number {
return Math.floor(totalmem() / (1024 * 1024));
}
function getGpuMemBudgetMb(): number {
const free = getFreeMb();
if (free < 2048) return 512;
if (free < 4096) return 1024;
const total = getTotalMemMb();
if (total < 4096) return 512;
if (total < 8192) return 1024;
return 4096;
}
function getLowMemoryFlags(): string[] {
const free = getFreeMb();
if (free >= 4096) return [];
const heapMb = free < 2048 ? 256 : 512;
const total = getTotalMemMb();
if (total >= 8192) return [];
const heapMb = total < 4096 ? 256 : 512;
return [`--js-flags=--max-old-space-size=${heapMb}`];
}