MTLBuffer not released - memory leakage?

Hi,

I have an issue that I have experienced with either a newly released metalcompute library for Python (https://github.com/baldand/py-metal-compute) but also in my own C-extensions of Metal through Swift for Python. It seems that the allocated buffers do not get released. Because of space constraints in these forums, you can take look at a working example at https://github.com/baldand/py-metal-compute/issues/19. My own implementations are relatively simple: A Swift function declared with a @_cdecl is imported in Python via ctypes.CDLL. This Swift function creates MTLBuffers, copy C arrays as input, executes the Metal kernel and copies results in C arrays. The MTLbuffers only live inside the Swift function, with no need to be managed by Python GC. When running and the Swift function is called N-times (as in thousands of times), the allocated memory visible in ActivityMontior continues to grow until running out of memory and a MTLBuffers cannot be created anymore.

I wonder if the Swift deallocator is having some sort of blockage while being called as a C extension in Python and if there is a way to force the deallocation once the call to Metal compute has been completed.

Any help would be highly appreciated, as I plan to use Metal for very long calculations, calling thousands of times a kernel during the life of a single process.

Cheers

Sam

Just for the ones curious about this issue, thanks to Github user baldand (who, btw, is developing a super cool Metal wrapper for Python, https://github.com/baldand/py-metal-compute) who pointed out that I needed to set setPurgeableState in my MTLBuffers to be sure they get released.

MTLBuffer not released - memory leakage?
 
 
Q