Memory leak in MPSCNNConvolution

I encounter memory leak in MPSCNNConvolution. This code create and destroy MPSCNNConvolution instance in a loop. I compiled with -objc-arc. So I excepted constant memory usage. But I observed increasing. How to prevent this memory leak?


Environment


macOS Mojave 10.14.6

MacBookPro 13-inch 2018

Intel Iris Plus Graphics 655 1536MB


Code


for(int j=0;j<10000;j++){   
  MPSCNNConvolutionDescriptor * conv1descriptor = [MPSCNNConvolutionDescriptor cnnConvolutionDescriptorWithKernelWidth:kernel_x kernelHeight:kernel_y inputFeatureChannels:mem_cast(src_ptr)->shape.z outputFeatureChannels:mem_cast(dst_ptr)->shape.z neuronFilter:filter];
   conv1descriptor.strideInPixelsX=stride_w;
   conv1descriptor.strideInPixelsY=stride_h;
   conv1descriptor.dilationRateX=dilation_w;
   conv1descriptor.dilationRateY=dilation_h;
   MPSCNNConvolution *conv1layer = [[MPSCNNConvolution alloc] initWithDevice:resource->m_device convolutionDescriptor:conv1descriptor kernelWeights:&weight->raw[0] biasTerms:bias flags:MPSCNNConvolutionFlagsNone];
  printf("allocated size: %d\n",[resource->m_device currentAllocatedSize]);
}

Output


allocated size: 1164173312

allocated size: 1164914688

allocated size: 1165656064

allocated size: 1166397440

allocated size: 1167138816

allocated size: 1167880192

allocated size: 1168621568

allocated size: 1169362944

allocated size: 1170104320

allocated size: 1170845696

allocated size: 1171587072

allocated size: 1172328448

allocated size: 1173069824

allocated size: 1173811200

allocated size: 1174552576

Replies

I added autoreleasepool manually and solved this problem.


for(int j=0;j<10000;j++){  
@autoreleasepool{
  MPSCNNConvolutionDescriptor * conv1descriptor = [MPSCNNConvolutionDescriptor cnnConvolutionDescriptorWithKernelWidth:kernel_x kernelHeight:kernel_y inputFeatureChannels:mem_cast(src_ptr)->shape.z outputFeatureChannels:mem_cast(dst_ptr)->shape.z neuronFilter:filter];  
  conv1descriptor.strideInPixelsX=stride_w;  
  conv1descriptor.strideInPixelsY=stride_h;  
  conv1descriptor.dilationRateX=dilation_w;  
  conv1descriptor.dilationRateY=dilation_h;  
  MPSCNNConvolution *conv1layer = [[MPSCNNConvolution alloc] initWithDevice:resource->m_device convolutionDescriptor:conv1descriptor kernelWeights:&weight->raw[0] biasTerms:bias flags:MPSCNNConvolutionFlagsNone];  
  printf("allocated size: %d\n",[resource->m_device currentAllocatedSize]);  
} 
}