Fix BSOD MEMORY_MANAGEMENT on low-RAM servers

- Dynamically compute JVM -Xmx based on system RAM instead of hardcoded 32g
- Reduce peak memory during session loading (inline JSON string, skip redundant clone)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Sucukdeluxe 2026-03-10 14:19:49 +01:00
parent 4b624693ec
commit 0a724aed71
3 changed files with 19 additions and 6 deletions

View File

@ -1409,7 +1409,10 @@ export class DownloadManager extends EventEmitter {
this.appSessionStartedAt = startedAt;
this.runtimePersistedTotalMs = Math.max(0, Number(settings.totalRuntimeAllTimeMs || 0));
this.runtimePersistedAt = startedAt;
this.session = cloneSession(session);
// loadSession already returns a fresh, standalone object graph — no need to
// deep-clone again. This avoids duplicating the entire session in memory at
// startup which can spike peak heap on low-RAM servers.
this.session = session;
this.itemCount = Object.keys(this.session.items).length;
this.storagePaths = storagePaths;
this.debridService = new DebridService(settings, {

View File

@ -89,6 +89,14 @@ let resolveExtractorCommandInFlight: Promise<string> | null = null;
const EXTRACTOR_RETRY_AFTER_MS = 30_000;
const DEFAULT_ZIP_ENTRY_MEMORY_LIMIT_MB = 256;
/** Compute a safe JVM -Xmx value based on available physical RAM.
* Reserves 4 GB for Windows + Electron + other processes, caps at 16 GB. */
function jvmMaxHeapArg(): string {
const totalGb = os.totalmem() / (1024 ** 3);
const heapGb = Math.max(1, Math.min(Math.floor(totalGb - 4), 16));
return `-Xmx${heapGb}g`;
}
const EXTRACTOR_PROBE_TIMEOUT_MS = 8_000;
const DEFAULT_EXTRACT_CPU_BUDGET_PERCENT = 80;
let currentExtractCpuPriority: string | undefined;
@ -1392,7 +1400,7 @@ function startDaemon(layout: JvmExtractorLayout): boolean {
"-Dfile.encoding=UTF-8",
`-Djava.io.tmpdir=${jvmTmpDir}`,
"-Xms1g",
"-Xmx32g",
jvmMaxHeapArg(),
"-XX:+UseG1GC",
"-XX:MaxGCPauseMillis=50",
"-cp",
@ -1640,7 +1648,7 @@ async function runJvmExtractCommand(
"-Dfile.encoding=UTF-8",
`-Djava.io.tmpdir=${jvmTmpDir}`,
"-Xms1g",
"-Xmx32g",
jvmMaxHeapArg(),
"-XX:+UseG1GC",
"-XX:MaxGCPauseMillis=50",
"-cp",

View File

@ -721,12 +721,14 @@ export function normalizeLoadedSessionTransientFields(session: SessionState): Se
function readSessionFile(filePath: string): SessionState | null {
try {
const raw = fs.readFileSync(filePath, "utf8");
const parsed = JSON.parse(raw) as unknown;
// Inline readFileSync into JSON.parse so the raw string is not bound to a
// variable and can be GC'd immediately — avoids holding the full JSON text
// and the parsed object graph in memory simultaneously.
const parsed = JSON.parse(fs.readFileSync(filePath, "utf8")) as unknown;
const session = normalizeLoadedSessionTransientFields(normalizeLoadedSession(parsed));
const pkgCount = Object.keys(session.packages).length;
const itemCount = Object.keys(session.items).length;
logger.info(`Session geladen: ${filePath} (${pkgCount} Pakete, ${itemCount} Items, ${raw.length} Bytes)`);
logger.info(`Session geladen: ${filePath} (${pkgCount} Pakete, ${itemCount} Items)`);
return session;
} catch (error) {
logger.error(`Session-Datei nicht lesbar: ${filePath}: ${String(error)}`);