Very basic question about Metal resource management

Perhaps this was discussed and I missed it or maybe I haven’t gotten to it yet but…

Is it safe to assume that Metal device resources associated with Swift objects like MTLBuffer and MTLTexture objects will automatically get cleaned up when ARC cleans up the Swift objects?

Is there any way to explicitly free up Metal resources?

This thought occurred to me when I was looking at the code to create a texture array. I would hope that the individual textures get released very quickly.


@cocheret - I’m assuming you’re asking about resources on the GPU? The GPU itself will take care of clearing resources.

If you’re asking about Swift objects, you can set them to nil, if you have made your object an optional.

I guess I didn’t ask my question well. I am indeed thinking about the resources on the GPU. But there are also Swift objects that refer to those resources on the GPU. The GPU resources and the Swift resources each have their own lifecycles, which I assume are coupled in some way.

I understand, in general, the lifecycle of Swift objects. What I don’t understand is how they are coupled to the corresponding resources on the GPU. For example, let’s say I load a MTLTexture that I’m going to use for a particular game level. Does the resource live on the GPU for as long as I keep the MTLTexture object referenced on the CPU? What determines when the GPU can release that texture? The same would go for vertex buffers I’m holding on the GPU and their corresponding MTLBuffer objects on the CPU.

Let’s say I decide I know I know longer need a particular MTLBuffer object and want to allocate a new one with new geometry to replace it? Is there an explicit way to tell the GPU I don’t need this anymore? Or is releasing the reference to the MTLBuffer object in Swift (letting it go out of scope or setting an optional to nil) sufficient to know the resource will be released on the GPU?

I suppose it would also be good to understand how Metal deals with multiple programs contending for GPU resources? Can one process force the GPU to discard the resources for another process? Does Metal automatically put resources back in place when a process needs them?

the gpu memory is not as persistent as the system memory is. you are right when saying that releasing the reference to the MTLBuffer object is sufficient to have the resource be released on the GPU as well. there is no other explicit way to do it.