From 873f0bb6efa99e74f6a3b184f6b27ef1bc994b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lozier?= Date: Sun, 18 Oct 2026 16:13:17 -0400 Subject: [PATCH] Remove GC.GetTotalMemory from gc.collect --- src/core/IronPython.Modules/gc.cs | 10 ++++------ src/core/IronPython/Runtime/PythonContext.cs | 8 +------- tests/suite/modules/system_related/test_gc.py | 2 +- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/core/IronPython.Modules/gc.cs b/src/core/IronPython.Modules/gc.cs index 29cdd3236..3fd8d55c2 100644 --- a/src/core/IronPython.Modules/gc.cs +++ b/src/core/IronPython.Modules/gc.cs @@ -44,13 +44,11 @@ public static object isenabled() { return ScriptingRuntimeHelpers.True; } - public static int collect(CodeContext context, int generation) { - return context.LanguageContext.Collect(generation); - } + public static void collect(CodeContext context, int generation) + => context.LanguageContext.Collect(generation); - public static int collect(CodeContext context) { - return collect(context, GC.MaxGeneration); - } + public static void collect(CodeContext context) + => collect(context, GC.MaxGeneration); public static void set_debug(object? o) { throw PythonOps.NotImplementedError("gc.set_debug isn't implemented"); diff --git a/src/core/IronPython/Runtime/PythonContext.cs b/src/core/IronPython/Runtime/PythonContext.cs index c4e4d5e94..434549350 100644 --- a/src/core/IronPython/Runtime/PythonContext.cs +++ b/src/core/IronPython/Runtime/PythonContext.cs @@ -3232,15 +3232,11 @@ public override IList GetCallSignatures(object obj) { /// Performs a GC collection including the possibility of freeing weak data structures held onto by the Python runtime. /// /// - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.GC.Collect")] - internal int Collect(int generation) { + internal void Collect(int generation) { if (generation > GC.MaxGeneration || generation < 0) { throw PythonOps.ValueError("invalid generation {0}", generation); } - // now let the CLR do it's normal collection - long start = GC.GetTotalMemory(false); - for (int i = 0; i < 2; i++) { GC.Collect(generation); GC.WaitForPendingFinalizers(); @@ -3251,8 +3247,6 @@ internal int Collect(int generation) { FunctionCode.CleanFunctionCodes(this, true); } } - - return (int)Math.Max(start - GC.GetTotalMemory(false), 0); } public DynamicDelegateCreator DelegateCreator { diff --git a/tests/suite/modules/system_related/test_gc.py b/tests/suite/modules/system_related/test_gc.py index 80dedf0c2..358496433 100644 --- a/tests/suite/modules/system_related/test_gc.py +++ b/tests/suite/modules/system_related/test_gc.py @@ -138,7 +138,7 @@ def test_isenabled(self): def test_collect(self): if is_cli: - i = gc.collect() # returns # of bytes collected, could be anything + self.assertIsNone(gc.collect()) # returns None, we don't track collected objects else: for debug in debug_list: gc.set_debug(debug)