Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "toolviper"
version = "0.1.1"
version = "0.1.2"
description = ""
authors = [
{name = "Joshua Hoskins", email="jhoskins@nrao.edu"},
Expand Down
38 changes: 38 additions & 0 deletions src/toolviper/utils/memory_management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def get_rss_gb():
import psutil, os

return psutil.Process(os.getpid()).memory_info().rss / 1e9
Comment on lines +1 to +4


def memory_setup(threshold: int = 131072):
Comment on lines +6 to +7
"""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():
Comment on lines +16 to +19
"""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")
Comment on lines +31 to +33
# 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
Loading