Return unused C heap pages to the OS where supported (glibc malloc_trim).
()
| 547 | return wrapper |
| 548 | |
| 549 | def trim_c_allocator_heap(): |
| 550 | """Return unused C heap pages to the OS where supported (glibc malloc_trim).""" |
| 551 | try: |
| 552 | import ctypes |
| 553 | import ctypes.util |
| 554 | |
| 555 | libc_name = ctypes.util.find_library("c") |
| 556 | if not libc_name: |
| 557 | return False |
| 558 | libc = ctypes.CDLL(libc_name) |
| 559 | if not hasattr(libc, "malloc_trim"): |
| 560 | return False |
| 561 | libc.malloc_trim(0) |
| 562 | return True |
| 563 | except Exception: |
| 564 | logger.debug("malloc_trim unavailable or failed", exc_info=True) |
| 565 | return False |
| 566 | |
| 567 | |
| 568 | def cleanup_memory(log_usage=False, force_collection=True): |
no outgoing calls