From db22b8f36b43e1b7d67fefad3e856d4b00db180a Mon Sep 17 00:00:00 2001 From: Jan-Willem Date: Thu, 12 Mar 2026 17:05:20 -0400 Subject: [PATCH 1/3] Memory management utils. --- src/toolviper/utils/memory_management.py | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/toolviper/utils/memory_management.py diff --git a/src/toolviper/utils/memory_management.py b/src/toolviper/utils/memory_management.py new file mode 100644 index 0000000..d32cd91 --- /dev/null +++ b/src/toolviper/utils/memory_management.py @@ -0,0 +1,36 @@ + +def get_rss_gb(): + import psutil, os + return psutil.Process(os.getpid()).memory_info().rss / 1e9 + + +def set_mmap_threshold(threshold: int): + """Set malloc mmap threshold to reduce heap fragmentation. + + On Linux this calls glibc's mallopt(M_MMAP_THRESHOLD, threshold). + On macOS the system allocator handles this automatically; the call is skipped. + """ + import sys, ctypes + + if sys.platform == "linux": + ctypes.CDLL("libc.so.6").mallopt(-3, threshold) + + +def malloc_trim(): + """Return free memory pages to the OS. + + On Linux this calls glibc's malloc_trim(0). + On macOS, malloc_zone_pressure_relief is used as the closest equivalent. + """ + import sys, ctypes + + if sys.platform == "linux": + ctypes.CDLL("libc.so.6").malloc_trim(0) + elif sys.platform == "darwin": + try: + lib = ctypes.CDLL("libSystem.B.dylib") + # malloc_zone_pressure_relief(zone=NULL, goal=0) asks all zones to + # release as much memory as possible back to the OS. + lib.malloc_zone_pressure_relief(None, 0) + except Exception: + pass From 510e19036f00b1d8a9b4dc969d73dbfc5184574a Mon Sep 17 00:00:00 2001 From: Jan-Willem Date: Tue, 17 Mar 2026 13:08:51 -0400 Subject: [PATCH 2/3] Add memory management utils. --- src/toolviper/utils/memory_management.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/toolviper/utils/memory_management.py b/src/toolviper/utils/memory_management.py index d32cd91..6ae6383 100644 --- a/src/toolviper/utils/memory_management.py +++ b/src/toolviper/utils/memory_management.py @@ -1,10 +1,13 @@ + + + def get_rss_gb(): import psutil, os return psutil.Process(os.getpid()).memory_info().rss / 1e9 -def set_mmap_threshold(threshold: int): +def memory_setup(threshold: int=131072): """Set malloc mmap threshold to reduce heap fragmentation. On Linux this calls glibc's mallopt(M_MMAP_THRESHOLD, threshold). @@ -16,14 +19,15 @@ def set_mmap_threshold(threshold: int): ctypes.CDLL("libc.so.6").mallopt(-3, threshold) -def malloc_trim(): +def free_memory(): """Return free memory pages to the OS. On Linux this calls glibc's malloc_trim(0). On macOS, malloc_zone_pressure_relief is used as the closest equivalent. """ import sys, ctypes - + import gc + gc.collect() if sys.platform == "linux": ctypes.CDLL("libc.so.6").malloc_trim(0) elif sys.platform == "darwin": From 7764c2a25f0b3b91b537256a71b2f5ab9526bb38 Mon Sep 17 00:00:00 2001 From: Jan-Willem Date: Thu, 19 Mar 2026 12:50:50 -0400 Subject: [PATCH 3/3] Black. --- pyproject.toml | 2 +- src/toolviper/utils/memory_management.py | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c457ed1..9f28017 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "toolviper" -version = "0.1.1" +version = "0.1.2" description = "" authors = [ {name = "Joshua Hoskins", email="jhoskins@nrao.edu"}, diff --git a/src/toolviper/utils/memory_management.py b/src/toolviper/utils/memory_management.py index 6ae6383..0be068b 100644 --- a/src/toolviper/utils/memory_management.py +++ b/src/toolviper/utils/memory_management.py @@ -1,13 +1,10 @@ - - - - def get_rss_gb(): import psutil, os + return psutil.Process(os.getpid()).memory_info().rss / 1e9 -def memory_setup(threshold: int=131072): +def memory_setup(threshold: int = 131072): """Set malloc mmap threshold to reduce heap fragmentation. On Linux this calls glibc's mallopt(M_MMAP_THRESHOLD, threshold). @@ -27,6 +24,7 @@ def free_memory(): """ import sys, ctypes import gc + gc.collect() if sys.platform == "linux": ctypes.CDLL("libc.so.6").malloc_trim(0)