Skip to content

The agent that grows with you

perf(mem_trim): run gc.collect() on every platform (macOS serve phys_footprint 5m 380.6 -> 368.9 MB)

赵桂雄b7e987909a77
diff --git a/hermes_cli/mem_trim.py b/hermes_cli/mem_trim.py
index 9ed5856193a..e11f4462099 100644
--- a/hermes_cli/mem_trim.py
+++ b/hermes_cli/mem_trim.py
@@ -154,8 +154,6 @@ def trim_memory(
global _last_trim_monotonic, _trim_call_count
with _trim_lock:
trim = _probe_glibc_malloc_trim()
- if trim is None:
- return False
now = time.monotonic()
cooldown = configured_cooldown if cooldown_seconds is None else _cooldown_seconds(cooldown_seconds)
since_last = now - _last_trim_monotonic
@@ -168,7 +166,8 @@ def trim_memory(
before = collect_memory_snapshot()
started = time.perf_counter()
gc.collect()
- trim_result = trim(0)
+ # Cycle collection is allocator-independent; only the page release needs glibc.
+ trim_result = trim(0) if trim is not None else 0
released = bool(trim_result)
after = collect_memory_snapshot()
duration_ms = (time.perf_counter() - started) * 1000
diff --git a/tests/hermes_cli/test_mem_trim.py b/tests/hermes_cli/test_mem_trim.py
index 6b798485903..086c3c146db 100644
--- a/tests/hermes_cli/test_mem_trim.py
+++ b/tests/hermes_cli/test_mem_trim.py
@@ -15,14 +15,6 @@ def _reset_trim_state(monkeypatch):
monkeypatch.setattr(mem_trim, "_trim_call_count", 0)
-def test_unsupported_allocator_is_noop_without_gc(monkeypatch):
- collect = Mock()
- monkeypatch.setattr(mem_trim.gc, "collect", collect)
-
- assert mem_trim.trim_memory(force=True, reason="test") is False
- collect.assert_not_called()
-
-
def test_config_kill_switch_overrides_force_from_config_file(monkeypatch, tmp_path):
from hermes_constants import reset_hermes_home_override, set_hermes_home_override
@@ -45,6 +37,16 @@ def test_config_kill_switch_overrides_force_from_config_file(monkeypatch, tmp_pa
+def test_gc_collect_runs_even_without_platform_primitive(monkeypatch):
+ # No page-release primitive (macOS, musl): the rate-limited gc.collect() still runs;
+ # only the success flag stays False.
+ collect = Mock()
+ monkeypatch.setattr(mem_trim.gc, "collect", collect)
+
+ assert mem_trim.trim_memory(force=True, reason="test") is False
+ collect.assert_called_once()
+
+
def test_collect_memory_snapshot_parses_linux_proc_status(monkeypatch):
# No ``sys.platform`` pin: the only platform check lives inside
# ``_read_proc_status``, which is replaced below — the subject here is