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 new file mode 100644 index 0000000..0be068b --- /dev/null +++ b/src/toolviper/utils/memory_management.py @@ -0,0 +1,38 @@ +def get_rss_gb(): + import psutil, os + + return psutil.Process(os.getpid()).memory_info().rss / 1e9 + + +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). + 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 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": + 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