From 290cfb33a9bd8c6b826120f962f9d4b37f0a1acb Mon Sep 17 00:00:00 2001 From: junkmd Date: Mon, 16 Nov 2026 08:00:46 +0900 Subject: [PATCH 1/4] feat: Add type hints to `find_memleak`. --- comtypes/test/find_memleak.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/comtypes/test/find_memleak.py b/comtypes/test/find_memleak.py index 3908a017..01a7f949 100644 --- a/comtypes/test/find_memleak.py +++ b/comtypes/test/find_memleak.py @@ -1,6 +1,8 @@ import gc +from collections.abc import Callable from ctypes import POINTER, Structure, WinDLL, WinError, byref, c_size_t, sizeof from ctypes.wintypes import BOOL, DWORD, HANDLE +from typing import Any ################################################################ @@ -45,7 +47,7 @@ def wss(): LOOPS = 10, 1000 -def find_memleak(func, loops=LOOPS): +def find_memleak(func: Callable[[], Any], loops: tuple[int, int] = LOOPS) -> int: # call 'func' several times, so that memory consumption # stabilizes: for j in range(loops[0]): From e1e80f973656534c8faedb2cd6caedd5fbb85d82 Mon Sep 17 00:00:00 2001 From: junkmd Date: Mon, 16 Nov 2026 08:00:46 +0900 Subject: [PATCH 2/4] test: Add an initial unstable test. --- comtypes/test/test_find_memleak.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 comtypes/test/test_find_memleak.py diff --git a/comtypes/test/test_find_memleak.py b/comtypes/test/test_find_memleak.py new file mode 100644 index 00000000..a6a02d03 --- /dev/null +++ b/comtypes/test/test_find_memleak.py @@ -0,0 +1,30 @@ +import gc +import unittest + +from comtypes.malloc import CoGetMalloc +from comtypes.test.find_memleak import find_memleak + + +class Test_find_memleak(unittest.TestCase): + def setUp(self): + gc.collect() + gc.collect() + gc.collect() + + def tearDown(self): + gc.collect() + gc.collect() + gc.collect() + + def test_find_memleak_working_set_no_leak(self): + def no_leak(): + malloc = CoGetMalloc() + ptr = malloc.Alloc(1024) + malloc.Free(ptr) + + leak_amount = find_memleak(no_leak) + self.assertEqual(leak_amount, 0) + + +if __name__ == "__main__": + unittest.main() From 215e82c09e6380d8efa994f2a4bdee2470b26d95 Mon Sep 17 00:00:00 2001 From: junkmd Date: Mon, 16 Nov 2026 08:00:46 +0900 Subject: [PATCH 3/4] test: Enhance `test_find_memleak` with `wss()` warmup to stabilize memory measurements. --- comtypes/test/test_find_memleak.py | 36 +++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/comtypes/test/test_find_memleak.py b/comtypes/test/test_find_memleak.py index a6a02d03..ae257579 100644 --- a/comtypes/test/test_find_memleak.py +++ b/comtypes/test/test_find_memleak.py @@ -2,7 +2,7 @@ import unittest from comtypes.malloc import CoGetMalloc -from comtypes.test.find_memleak import find_memleak +from comtypes.test.find_memleak import find_memleak, wss class Test_find_memleak(unittest.TestCase): @@ -22,7 +22,41 @@ def no_leak(): ptr = malloc.Alloc(1024) malloc.Free(ptr) + # Call `wss()` once to absorb one-time initialization costs + # (COM allocator warmup, first page faults, etc.) before the + # actual measurement baseline is taken inside `find_memleak()`. + # + # `GetProcessMemoryInfo` (`wss`) itself is a pure read-only + # operation with no side effects on the working set. The + # stabilization effect comes from running through `find_memleak()` + # internal warmup/measurement cycle once, which exercises: + # + # 1. COM allocator (`CoGetMalloc`) first-use overhead: + # - Initial `Alloc`/`Free` calls fault in heap pages and + # initialize internal free lists. This temporarily + # inflates the working set. + # + # 2. Python/COM interop and ctypes first-use initialization: + # - First COM object access triggers one-time memory + # allocations for type info, vtables, etc. + # + # 3. OS page fault resolution timing: + # - Even after `gc.collect()`, physical memory commitment + # and working set accounting may lag slightly. + # + # By calling `wss()` once before the real test, these one-time + # costs are paid before the real baseline is captured, so the + # second measurement reflects only steady-state behavior + # (which should show zero net growth for a leak-free function). + _ = wss() + + # Now that the one-time costs have been paid, the allocator and + # working set are stabilized. The warmup phase inside + # `find_memleak()` no longer causes net memory growth, so the + # measured difference between baseline and final should be zero. leak_amount = find_memleak(no_leak) + + # The leak amount should be 0 (no net memory increase) self.assertEqual(leak_amount, 0) From 2d4e441b63e4b35cc97c9039cb730279dbb5713f Mon Sep 17 00:00:00 2001 From: junkmd Date: Mon, 16 Nov 2026 08:00:46 +0900 Subject: [PATCH 4/4] test: Refactor comments in `test_find_memleak` for clarity on `wss()` usage. --- comtypes/test/test_find_memleak.py | 46 ++++++++++-------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/comtypes/test/test_find_memleak.py b/comtypes/test/test_find_memleak.py index ae257579..7353c70a 100644 --- a/comtypes/test/test_find_memleak.py +++ b/comtypes/test/test_find_memleak.py @@ -22,41 +22,25 @@ def no_leak(): ptr = malloc.Alloc(1024) malloc.Free(ptr) - # Call `wss()` once to absorb one-time initialization costs - # (COM allocator warmup, first page faults, etc.) before the - # actual measurement baseline is taken inside `find_memleak()`. + # `find_memleak()` takes its baseline with one `wss()` call and its + # final reading with another. The first `wss()` call in a process + # incurs one-time costs (e.g. first-time code paths faulting in + # pages) that land between those two readings, so a cold probe can + # make an otherwise leak-free function look like it grew by a page. + # Calling `wss()` once here pays that cost beforehand. # - # `GetProcessMemoryInfo` (`wss`) itself is a pure read-only - # operation with no side effects on the working set. The - # stabilization effect comes from running through `find_memleak()` - # internal warmup/measurement cycle once, which exercises: + # Note that this only warms up the probe itself. The warmup of + # `no_leak()` is already done by `find_memleak()`. # - # 1. COM allocator (`CoGetMalloc`) first-use overhead: - # - Initial `Alloc`/`Free` calls fault in heap pages and - # initialize internal free lists. This temporarily - # inflates the working set. - # - # 2. Python/COM interop and ctypes first-use initialization: - # - First COM object access triggers one-time memory - # allocations for type info, vtables, etc. - # - # 3. OS page fault resolution timing: - # - Even after `gc.collect()`, physical memory commitment - # and working set accounting may lag slightly. - # - # By calling `wss()` once before the real test, these one-time - # costs are paid before the real baseline is captured, so the - # second measurement reflects only steady-state behavior - # (which should show zero net growth for a leak-free function). + # This is a workaround that makes the measurement stable in practice. + # It does not address the fundamental limitation of Working Set + # based measurement (see https://github.com/enthought/comtypes/issues/971): + # the Working Set is the amount of resident pages, not the amount of + # unreleased memory, so a stable result does not prove the absence of + # a leak. _ = wss() - - # Now that the one-time costs have been paid, the allocator and - # working set are stabilized. The warmup phase inside - # `find_memleak()` no longer causes net memory growth, so the - # measured difference between baseline and final should be zero. + # Actual measurement. `find_memleak` already warms up `no_leak`. leak_amount = find_memleak(no_leak) - - # The leak amount should be 0 (no net memory increase) self.assertEqual(leak_amount, 0)