[GC] total memory: 81081118720 [GC] occupied memory: 54716248088 [GC] stack scans: 557345 [GC] stack cells: 77 [GC] cycle collections: 8 [GC] max threshold: 78399625696 [GC] zct capacity: 1024 [GC] max cycle table size: 0 [GC] max pause time [ms]: 0 [GC] max stack size: 4656 [GC] total memory: 81081118720 [GC] occupied memory: 6419767248 [GC] stack scans: 557346 [GC] stack cells: 77 [GC] cycle collections: 9 [GC] max threshold: 78399625696 [GC] zct capacity: 1024 [GC] max cycle table size: 0 [GC] max pause time [ms]: 0 [GC] max stack size: 4656
which is OK from Nim's point of view. The operating system, however, still thinks the process uses more than 50 GB of memory. Now the code calls an external library, and the library failed at malloc at some point, which ends up killing the whole process.
How can I actually release the memory?
Short answer you can't.
Nim will keep the free memory for itself in case it needs to use it.
Many libraries have a way to supply it a memory allocator: https://github.com/nim-lang/Nim/blob/version-1-2/lib/wrappers/openssl.nim#L495
@jxy I've used deallocHeap in the past:
deallocHeap(runFinalizers = true, allowGcAfterwards = false)
This is exactly the kind of thing gc:arc is designed to fix.
-gc:arc -d:useMalloc c yourProgram.nim
GC arc is deterministic (meaning memory will be freed immediately after Nim thinks it's done with it, i.e. C++ destructors) and useMalloc will force nim to use the system malloc / free, so memory will be directly used and returned to the operating system, without the Nim runtime holding onto the memory.