Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

BlobBuilder Leaks memory in CreateBlobAssetReference

Discussion in 'Entity Component System' started by JonasDeMaeseneer, Jul 2, 2019.

  1. JonasDeMaeseneer

    JonasDeMaeseneer

    Joined:
    Oct 4, 2018
    Posts:
    4
    When executing the following code I get en error in editor which states there was a memory leak.

    Code (CSharp):
    1.             using (BlobBuilder builder = new BlobBuilder(Allocator.Persistent))
    2.  
    3.             {
    4.  
    5.                 ref BlobArray<float> cachedData = ref builder.ConstructRoot<BlobArray<float>>();
    6.  
    7.                 BlobBuilderArray<float> cachedValues = builder.Allocate(AmountSamples, ref cachedData);
    8.  
    9.                 for (int i = 0; i < AmountSamples; i++)
    10.  
    11.                 {
    12.  
    13.                     cachedValues[i] = realCurve.Evaluate((float) i / AmountSamples);
    14.  
    15.                 }
    16.  
    17.                 _cachedAnimData = builder.CreateBlobAssetReference<BlobArray<float>>(Allocator.Persistent);
    18.  
    19.             }

    I looked into the CreateBlobAssetReference code and found that the native array "offsets" never got disposed. I changed the code so it disposes it nicely. This fixes the memory leaks.

    What I find strange is that this is mentioned nowhere on the forum even though people have asked questions with BlobBuilder code in it.
    Am I doing something wrong?

    Extra question: Wether i release the _cachedAnimData at the end of the game or not doesn't change anything, but also doesn't error when i don't release. Is releasing the blobassetreference mandatory?
     
  2. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    888
  3. ilih

    ilih

    Joined:
    Aug 6, 2013
    Posts:
    1,320
    JonasDeMaeseneer likes this.
  4. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    888
    Sorry, was my fault. Didn't recognized it was DOTS in new threads.
     
    Last edited: Jul 2, 2019
  5. JonasDeMaeseneer

    JonasDeMaeseneer

    Joined:
    Oct 4, 2018
    Posts:
    4
    The fix i mentioned is still correct afaik, but for the BlobBuilder it's better to use a Temp Allocator.
    If you do this, it doesn't leak memory in any case.
    But the memory leak is still something that will need to be fixed in a future release.
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Thanks. Should be fixed in next release.