feat: enhance memory management and session handling in database operations

- Introduced a shielded async session context manager to ensure safe session closure during cancellations.
- Updated various database operations to utilize the new shielded session, preventing orphaned connections.
- Added environment variables to optimize glibc memory management, improving overall application performance.
- Implemented a function to trim the native heap, allowing for better memory reclamation on Linux systems.
This commit is contained in:
DESKTOP-RTLN3BA\$punk 2026-02-28 23:59:28 -08:00
parent dd3da2bc36
commit ecb0a25cc8
7 changed files with 76 additions and 17 deletions

View file

@ -149,3 +149,26 @@ def log_system_snapshot(label: str = "system_snapshot") -> None:
snap["rss_delta_mb"],
snap["rss_mb"],
)
def trim_native_heap() -> bool:
"""Ask glibc to return free heap pages to the OS via ``malloc_trim(0)``.
On Linux (glibc), ``free()`` does not release memory back to the OS if
it sits below the brk watermark. ``malloc_trim`` forces the allocator
to give back as many freed pages as possible.
Returns True if trimming was performed, False otherwise (non-Linux or
libc unavailable).
"""
import ctypes
import sys
if sys.platform != "linux":
return False
try:
libc = ctypes.CDLL("libc.so.6")
libc.malloc_trim(0)
return True
except (OSError, AttributeError):
return False