igl icon indicating copy to clipboard operation
igl copied to clipboard

【Metal】MTLBuffer was not properly released

Open vinsentli opened this issue 5 months ago • 2 comments

I noticed that even though igl::meta::Buffer is released after running the program for a while, MTLBuffer is not properly released, and the memory keeps increasing. For example, in the screenshot below, there are only 298 objects in igl::metal::Buffer, but CaptureMTLBuffer still has 3882 objects.

One solution I found is to call [MTLBuffer setPurgeableState:MTLPurgeableStateEmpty], which prevents the continuous memory growth even though the number of CaptureMTLBuffer objects doesn't decrease.

My solution:

Buffer::~Buffer(){
  for (auto & buf : mtlBuffers_){
    [buf setPurgeableState:MTLPurgeableStateEmpty];
    buf = nil;
  }
  mtlBuffers_.clear();
}

However, [MTLBuffer setPurgeableState:MTLPurgeableStateEmpty] is typically used in non-ARC environments and theoretically should not be necessary in an ARC environment.

image

vinsentli avatar Sep 20 '24 04:09 vinsentli