Search code examples
iosxamarinxamarin.iosmetalkit

Metal texture doesn't released in Xamarin


This text was edited after extended investigation and double checks.

I'm developing an enterprise (not game) app based on Metal API in Xamarin, and I have critical issue: Metal textures aren't releasing it's memory after dispose, what is leading to extensive memory leaks and app crashes.

class SomeClass
{
    public void CreateTexture()
    {
        var metalTexture = device.CreateTexture(textureDescriptor);
        metalTexture.Dispose();
    }
}

Above you can see a simplified sample, which are demonstrates the issue. A texture's memory doesn't release after Dispose() call, even when texture is a local variable.

Can anybody explain what is going on, and how to release texture's memory?

It looks like Metal Kit support in Xamarin is totally broken and useless because of this issue. And I'm deeply shocked: WTF is going on?! How is it possible for Xamarin team to make initially broken feature and don't fix the issue for years? What are you doing there, guys? Xamarin forums is totally dead. Currently, this proudly called "Visual studio for Mac", but what's the point? Damn code snippets are still buggy – have to delete auto-inserted parenthesis to fix the cursor.

P.S.
Here is the repo with the project demonstrating the problem:
https://github.com/kav-git/Xamarin-Metal-Issue

  1. Memory usage is logged into Output window of IDE during debug.
  2. Press Add texture button to add new 64Mb texture to collection.
  3. Press Dispose textures button to purge all textures. No memory will be released.
  4. Press Release pixelData button to ensure GC is OK when you have deal with non-texture objects.
  5. Use GC.Collect() button just for fun.

Solution

  • After hours of random search for a solution, the one was found fortunately.

    Adding a call to
    metalTexture.SetPurgeableState(MTLPurgeableState.Empty);
    right before disposing a texture solves the problem.

    According to number of answers, I'm feeling myself the only guy in the World, who's using Xamarin with Metal Kit...