Search Unity

  1. How has 2019.2 and the beta been for you so far? Give us feedback in this thread.
    Dismiss Notice

[Solved] A Native Collection has not been disposed, resulting in a memory leak.

Discussion in 'Data Oriented Technology Stack' started by Antypodish, Aug 31, 2018.

  1. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,816
    So I have receiving none critical error at OnCreateManager () after NativeArray allocation.

    A Native Collection has not been disposed, resulting in a memory leak C:\User\ .....

    None critical, as code runs.

    To simplyfy

    Code (CSharp):
    1. // for now
    2. static public List <Nodes> octrees = new List <Nodes> () ;
    3.  
    4. public struct Nodes
    5. {
    6.     public int lastNodeIndex ;
    7.     public NativeArray <Bounds> bounds ;
    8.     ...
    9. }
    10.  
    11. protected override void OnCreateManager (int capacity)
    12. {
    13. NodesData octree = new NodesData () ;
    14. octree.bounds = new NativeArray<Bounds> ( 100, Allocator.Persistent ) ;
    15. ...
    16. // for now storing in the list
    17. octrees.Add ( octree ) ;
    18.  
    19. // If I try add dispose, it throws error (see spoiler for full description)
    20. // InvalidOperationException: The native container has been declared as [WriteOnly] in the job, but you are reading from it.
    21. octree.a_bounds.Dispose () ;
    22.  
    23. }
    InvalidOperationException: The native container has been declared as [WriteOnly] in the job, but you are reading from it.
    Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrowNoEarlyOut (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) <0x1c7831c0 + 0x00052> in <f718fbf7147e402282fdacf37bdd8584>:0
    Unity.Collections.NativeArray`1[T].CheckElementReadAccess (System.Int32 index) (at C:/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:113)
    Unity.Collections.NativeArray`1[T].get_Item (System.Int32 index) (at C:/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:134)
    ECS.Octree.Point.BoundingOctreeAddNodeSystem._Add2RootNode (System.Int32 i_octreeIndex, System.Int32 i_nodeIndex, ECS.Octree.EntityInstance entityInstance, ECS.Octree.Bounds entityBounds) (at Assets/Project/Scripts/Octree/Systems/BoundingOctreeAddNodeSystem.cs:455)
    ECS.Octree.Point.BoundingOctreeAddNodeSystem._AddEntity (System.Int32 i_octreeIndex, ECS.Octree.EntityInstance entityInstance) (at Assets/Project/Scripts/Octree/Systems/BoundingOctreeAddNodeSystem.cs:418)
    ECS.Octree.Point.BoundingOctreeAddNodeSystem+AddNode.Execute () (at Assets/Project/Scripts/Octree/Systems/BoundingOctreeAddNodeSystem.cs:268)
    Unity.Jobs.IJobExtensions+JobStruct`1[T].Execute (T& data, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at C:/buildslave/unity/build/Runtime/Jobs/Managed/IJob.cs:30)

    Without dispose it works. However it throws errors at initialization, then let code run.
    What I am missing?
    [ReadOnly] ?

    Is the List an issue?
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,238
    The dispose call has to be done when you close the application or have no more use for the bounds array in the octrees node list.

    if you dispose it right away you will get errors whenever you try to use the NodesData since the array is disposed.
     
    chris_gong and Antypodish like this.
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,816
    Ah ..
    Silly me. Thx lot.

    Added

    Code (CSharp):
    1. protected override void OnDestroyManager ( )
    2. {
    3. .. dispose calls
    4. }
    This solves the issue.
    So error was actually from previous simulation. Makes sense.
     
    chris_gong likes this.
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,816
    I would like indicate one more thing upon that error message, since it came back, after migrating to latest Entities preview 11.

    It is more feature request.

    It would be nice, if such error ..

    A Native Collection has not been disposed, resulting in a memory leak. It was allocated at C:\Users\...\AppData\Local\Unity\cache\packages\packages.unity.com\com.unity.collections@0.0.9-preview.3\Unity.Collections\NativeList.cs:24.


    ... could point into a script, where the error is generated. I got multiple scripts in project with native arrays and not sure, where the error is called from. Had no errors before upgrade, so now I need check manually every possible location, where dispose may be missing.
     
  5. simonm_unity

    simonm_unity

    Unity Technologies

    Joined:
    Mar 21, 2018
    Posts:
    13
    I have it on my todo list to add an option to track the callstack of these allocations. That should make them easy to track down. I should be able to get to this soon!
     
    starikcetin likes this.
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,816
    Most of them have some hints (# line of code) to allow track them down. Or even double click works.
    But still having from to time, these which are hard to track down.

    Anyway, looking forward to an update.
    Thx
     
  7. PieterPaladin

    PieterPaladin

    Joined:
    Nov 25, 2015
    Posts:
    2
    Hi Simon,

    Do you know if there is any progress on this feature?

    Thanks!

    Pieter
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    4,816
    With latest Entities 0.0.12 preview 20, error shows now the system *.cs file name and the job line.
    This is big help already. Thx @simonm_unity
    However, error do not indicates, which NativeArray may cause the problem.
    So if you have more than one array, this may become tedious to find source of issue.
     
  9. simonm_unity

    simonm_unity

    Unity Technologies

    Joined:
    Mar 21, 2018
    Posts:
    13
    Support for full stack traces is done (not by me) and should make it into 2019.1.
    So hopefully you should be able to use this soon.
     
    starikcetin, Rewaken, Zoey_O and 2 others like this.
  10. TieSKey

    TieSKey

    Joined:
    Apr 14, 2011
    Posts:
    21
    How do I enable the full stack trace on 2019.2.0a4? I get a bunch of "
    A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details." without any traces, just that line. Already tried enabling it from the console menu and player settings, even disabled burst but still the same.
     
    learc83 likes this.
  11. tree_fiddler_2000

    tree_fiddler_2000

    Joined:
    Dec 5, 2018
    Posts:
    15
    Any follow up from the above? I'm on 2019.1.0b4, can't see a way to turn it on... setting somewhere?
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,714
    Main Menu > Jobs > Leak Detection > Full Stack Traces (Expensive)
     
    starikcetin likes this.
  13. tree_fiddler_2000

    tree_fiddler_2000

    Joined:
    Dec 5, 2018
    Posts:
    15
    I don't seem to have that menu in 2019.1.0b4. I tried installing the jobs package (0.0.7) but it doesn't seem to compile on 2019.1.
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,714
    Try 2019.1.0b5.
     
  15. TieSKey

    TieSKey

    Joined:
    Apr 14, 2011
    Posts:
    21
    That menu option comes with the Jobs package. Good luck finding the right combination of versions to make it compile. Wiping the shader and package cache does help sometimes.