Management of Metal resources

There some MTLTextures of which histogram is to be calculated, this textures changes at every loop iteration so calculation of histogram is carried out in loop using MPSImageHistogram.

There is a single command queue on which a new command buffer is created at every loop iteration to perform histogram calculation.

Memory footprints (from allocation instrument) keeps on increasing due to new command buffer creation in the loop.

The question is how to clear the allocated memory of the command buffer once it's executed? Or is there any way for restructuring the calculation scheme?

In short, how to deallocate the memory consumed by metal objects like command buffer, compute pipeline, encoders etc.

Answered by Engineer in 681834022

Hi, a command buffer and encoders are autoreleased object. So one way to release them would be to have an autoreleasepool per loop iteration.

autoreleasepool 
{
    id<MTLCommandBuffer> cb = [queue commandBuffer];
    // encode work
    [cb commit];
}
Accepted Answer

Hi, a command buffer and encoders are autoreleased object. So one way to release them would be to have an autoreleasepool per loop iteration.

autoreleasepool 
{
    id<MTLCommandBuffer> cb = [queue commandBuffer];
    // encode work
    [cb commit];
}
Management of Metal resources
 
 
Q