Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.jscycles.js:
cycles-small.js, run with
qjs --memory-limit 204800 cycles-small.js: