Skip to content

Run the cycle collector before the memory limit is reached - #622

Open
ecochard wants to merge 1 commit into
bellard:masterfrom
ecochard:gc-threshold-limit
Open

ecochard wants to merge 1 commit into
bellard:masterfrom
ecochard:gc-threshold-limit

Conversation

@ecochard

Copy link
Copy Markdown

Hello,

On small heaps, QuickJS never runs its cycle collector. The automatic GC starts when malloc_size passes malloc_gc_threshold, which is 256 KB at first and then 1.5 x the heap after each GC.
A runtime whose memory limit is below 256 KB therefore never reaches it: every cycle (an object and a closure that refer to each other, for example) stays until the heap is full and the allocation fails. The same thing happens, more slowly, when the heap is close to its limit: 1.5 x the current size can be above the limit, so the heap fills up before the next GC.

I met this on an ESP32-S3 without PSRAM, where the JS heap is a 248 KB region: a program that made closures in a loop ran out of memory although it held almost nothing alive.

Proposed fix (patch attached, against quickjs.c 2026-06-04):
the threshold of the next GC is capped halfway from the current size to the memory limit, when a limit is set, and JS_SetMemoryLimit lowers the current threshold the same way (unless automatic GC is off, threshold = -1).

Without a memory limit nothing changes.

Reproduce with the two scripts bellow and the original qjs:

qjs --memory-limit 1048576 cycles.js
original: InternalError: out of memory
patched: ok: 60000 cycles made, 450 live pieces kept

qjs --memory-limit 204800 cycles-small.js
original: out of memory (prints "null")
patched: ok: 60000 cycles made, 0 live pieces kept

cycles.js keeps about 450 KB alive and makes 60,000 short-lived cycles;
cycles-small.js makes the same cycles with nothing alive.

The tests of the release (test_closure, test_language, test_builtin, test_loop, test_bigint, test_cyclic_import, test_worker) pass with the patch.

Best regards,

To reproduce: qjs --memory-limit 1048576 cycles.js

cycles.js:

// Run with a small memory limit: qjs --memory-limit 1048576 cycles.js
// About 70 % of the heap holds live data, then short-lived cycles are made
// (an object and a closure that refer to each other), many times what the
// heap holds. They are garbage, but only the cycle collector can free them.
const live = [];
for (let i = 0; i < 450; i++)
    live.push(new Uint8Array(1000));

function makeCycle(index) {
    const holder = { index, payload: new Array(16).fill(index) };
    holder.done = () => holder.index;
    return holder.done();
}

let made = 0;
for (let i = 0; i < 60000; i++)
    made += makeCycle(i) === i ? 1 : 0;
print(`ok: ${made} cycles made, ${live.length} live pieces kept`);

cycles-small.js, run with qjs --memory-limit 204800 cycles-small.js:

// Run with a small memory limit: qjs --memory-limit 1048576 cycles.js
// About 70 % of the heap holds live data, then short-lived cycles are made
// (an object and a closure that refer to each other), many times what the
// heap holds. They are garbage, but only the cycle collector can free them.
const live = [];
for (let i = 0; i < 0; i++)
    live.push(new Uint8Array(1000));

function makeCycle(index) {
    const holder = { index, payload: new Array(16).fill(index) };
    holder.done = () => holder.index;
    return holder.done();
}

let made = 0;
for (let i = 0; i < 60000; i++)
    made += makeCycle(i) === i ? 1 : 0;
print(`ok: ${made} cycles made, ${live.length} live pieces kept`);

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