I know how to create a MTLBuffer and or MTLTexture but how do I free the GPU memory for these resources when they are no longer needed?
MTLBuffer
and MTLTexture
are Objective-C objects and thus reference counted. If you are using automatic reference counting in an Objective-C project or using Metal via Swift, simply ensuring you no longer hold references to the buffer or texture will release any associated hardware resources.
let texture: MTLTexture? = device.newTexture(with: descriptor)
texture = nil // <- resources will be released
One can confirm this by stepping through the associated assembly when assigning nil
to texture
, which first leads us to [MTLDebugTexture dealloc]
MetalTools`-[MTLDebugTexture dealloc]:
...
-> 0x100af569e <+34>: call 0x100af87ee ; symbol stub for: objc_msgSendSuper2
0x100af56a3 <+39>: add rsp, 0x10
0x100af56a7 <+43>: pop rbp
0x100af56a8 <+44>: ret
...and through [MTLToolsObject dealloc]
MetalTools`-[MTLToolsObject dealloc]:
0x100ac6c7a <+0>: push rbp
0x100ac6c7b <+1>: mov rbp, rsp
0x100ac6c7e <+4>: push r14
...
...and through the GeForceMTLDriver
GeForceMTLDriver`___lldb_unnamed_symbol1095$$GeForceMTLDriver:
-> 0x7fffd2e57b14 <+0>: push rbp
0x7fffd2e57b15 <+1>: mov rbp, rsp
All the way, releasing any resources through the various dealloc
methods.