Search Unity

Unity Physics BlobAsset Deallocation questions

Discussion in 'Entity Component System' started by recursive, May 26, 2019.

  1. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    Hello.

    I'm writing my own non-physics related code to convert ScriptableObject data to BlobAssetReference<T> and I'm wondering how Unity Physics Releases the memory. As far as I can tell, the Release() function is called anywhere in the sample demos and there doesn't seem to be any other magic.

    Do persistent allocations made by BlobAllocator/BlobReference go into a special pool that's released for domain reloads? Is there a memory leak? Is there some other cleanup code I've overlooked?

    More "Best practices"/documentation with Blob Assets would be appreciated.
     
    Orimay and SamOld like this.
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Blobs are grouped together into one mega blob per scene at conversion time. Each scene references the pointer of the single allocation of all blobs from the converted scene. This is referenced by a shared component, when the last entity using that shared component / scene gets unloaded then the memory is unloaded.

    The result of it is that the cost of memory management for lots of small blobs is practically zero. This is great for streaming massive amounts of content without any perf impact. Also it means that specific code doesn't have to think about allocating / deallocation of blob data. Hence physics has no code that does it explicitly.
     
    Orimay, recursive and SamOld like this.
  3. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    333
    Does all of this only apply to the conversion workflow? If I make a custom blob (for example by using the factory methods to create a collider) does the engine at some point take on responsibility for destroying that or does that still need manual release? Are you saying that any blob assigned to a shared component is released alongside the component?

    I agree with @recursive that we need more documentation on blobs.
     
    Orimay and recursive like this.
  4. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    To add to what @SamOld put. I'd like to setup something akin to ScriptableObject's scope for certain types of blob data: A shared pointer that's alive as long as something in *any* scene references it and is deleted when the last reference is changed or deleted.