From 78a49d7df4e370106d154521fad7ad5eeaf3ea16 Mon Sep 17 00:00:00 2001 From: Ivo Horak Date: Sun, 29 Nov 2026 05:06:20 +0100 Subject: [PATCH 1/2] Cap the young generation of the native standalone at 16 MB The serial collector of the native image starts with an initial heap of 128 MB, which gives a young generation of about 48 MB, and lets the young generation grow to 10% of physical memory. The whole young generation is touched before the first collection and its pages stay resident, so even a program that keeps little alive holds tens of MB of garbage pages, on top of what it keeps. Build libpythonvm with MaxNewSize=16 MB as the default. More young collections happen in allocation-heavy code; --vm.Xmn overrides the default per process. --- mx.graalpython/mx_graalpython.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mx.graalpython/mx_graalpython.py b/mx.graalpython/mx_graalpython.py index f2e85d7998..a01027a899 100644 --- a/mx.graalpython/mx_graalpython.py +++ b/mx.graalpython/mx_graalpython.py @@ -527,6 +527,13 @@ def libpythonvm_build_args(): # MAP_JIT memory is mapped next to the (ASLR-slid) dylibs. build_args += ['-H:-RandomizeRuntimeCodeCache'] + if not graalos and not _is_overridden_native_image_arg("-R:MaxNewSize"): + # The serial collector starts with a 128 MB heap and a young generation of about 48 MB, and is + # allowed to grow the young generation to 10% of physical memory. Every page of the young + # generation is touched before the first collection and stays resident, so a small program + # keeps tens of MB of garbage. A 16 MB young generation bounds that; --vm.Xmn overrides it. + build_args += ['-R:MaxNewSize=16777216'] + profile = None require_profile = get_boolean_env("GRAALPY_REQUIRE_PGO_PROFILE") if ( From 99c1b804e11dfaa8d97964881ee5be0c7a193488 Mon Sep 17 00:00:00 2001 From: Ivo Horak Date: Sun, 29 Nov 2026 05:06:30 +0100 Subject: [PATCH 2/2] Compile in the main isolate in the native standalone Truffle compilations of the native standalone run in a separate isolate by default. That isolate has its own heap, which starts with a young generation of about 48 MB that is touched before its first collection, and it maps and touches the image heap a second time. Compile in the main isolate instead; the compiler's allocations then share the main heap and its young generation. --vm.XX:+CompileInIsolates restores the separate isolate per process. --- mx.graalpython/mx_graalpython.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mx.graalpython/mx_graalpython.py b/mx.graalpython/mx_graalpython.py index a01027a899..af47f26391 100644 --- a/mx.graalpython/mx_graalpython.py +++ b/mx.graalpython/mx_graalpython.py @@ -534,6 +534,12 @@ def libpythonvm_build_args(): # keeps tens of MB of garbage. A 16 MB young generation bounds that; --vm.Xmn overrides it. build_args += ['-R:MaxNewSize=16777216'] + if not graalos and not _is_overridden_native_image_arg("-R:+CompileInIsolates") and not _is_overridden_native_image_arg("-R:-CompileInIsolates"): + # Runtime compilation runs in a separate isolate by default, with its own heap (which starts + # with a young generation of about 48 MB) and a second mapping of the image heap that it + # touches. Compile in the main isolate instead; --vm.XX:+CompileInIsolates restores it. + build_args += ['-R:-CompileInIsolates'] + profile = None require_profile = get_boolean_env("GRAALPY_REQUIRE_PGO_PROFILE") if (