Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Bug Memory leak when copying native queue to native array

Discussion in '2019.3 Beta' started by ZenDraL, Aug 30, 2019.

  1. ZenDraL

    ZenDraL

    Joined:
    Jan 16, 2014
    Posts:
    3
    I have this system that runs fine in 3.0a5 but when I upgrade it just eats up more and more memory until it crashes the computer. I've reduced it to the following example. The longer it runs the larger NativeArray gets in the memory profile. As far as I can tell I'm not doing anything wrong here. All the arrays are disposed of.

    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Entities;
    3. using Unity.Jobs;
    4.  
    5. public class FillArrayFromQueueSystem : ComponentSystem
    6. {
    7.     public struct FillArrayJob<T> : IJob where T : struct
    8.     {
    9.         internal NativeQueue<T> Queue;
    10.         internal NativeArray<T> Array;
    11.         public void Execute()
    12.         {
    13.             int index = 0;
    14.             T item;
    15.             while (Queue.TryDequeue(out item))
    16.             {
    17.                 Array[index] = item;
    18.                 index++;
    19.             }
    20.         }
    21.     }
    22.    
    23.     protected override void OnUpdate()
    24.     {
    25.  
    26.         NativeQueue<int> queue = new NativeQueue<int>(Allocator.TempJob);
    27.         for (int i = 0; i < 100000; i++)
    28.         {
    29.             queue.Enqueue(i);
    30.         }
    31.         NativeArray<int> array = new NativeArray<int>(queue.Count, Allocator.TempJob);
    32.         var fillJob = new FillArrayJob<int>
    33.         {
    34.             Queue = queue,
    35.             Array = array
    36.         }.Schedule();
    37.         fillJob.Complete();
    38.  
    39.         queue.Dispose();
    40.         array.Dispose();
    41.     }
    42. }
    43.  


     
    ivan2017 likes this.
  2. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    866
    You should make a bug report and post Issue ID here.
     
    Peter77 likes this.
  3. ZenDraL

    ZenDraL

    Joined:
    Jan 16, 2014
    Posts:
    3
    Case 1177449
     
    LeonhardP and Peter77 like this.
  4. bazomatic

    bazomatic

    Joined:
    Jun 24, 2019
    Posts:
    4
    Replicated this too, in 2019.1.8f1, but I can't seem to find case 1177449 to vote for it...?
     
    ZenDraL likes this.
  5. ZenDraL

    ZenDraL

    Joined:
    Jan 16, 2014
    Posts:
    3
    Last edited: Sep 14, 2019
  6. bazomatic

    bazomatic

    Joined:
    Jun 24, 2019
    Posts:
    4
    Same, I'm really glad you posted this since it took me a bit to realize it was the conversion from NativeQueue to NativeArray that was causing my issue and I was losing my mind, lol. No errors or anything, just monitoring memory usage reveals the ever-increasing allocations that are never deallocated, totally unclear why something that should not be referenced/in scope was holding onto memory.

    EDIT: This issue is unrelated to jobs, by the way. I took the same code and just inlined it, same exact issue, at least in 2019.1.8f1, so it appears to be an issue with either NativeArray or NativeQueue itself.
     
    ZenDraL likes this.
  7. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    They refactored some of the internal queue implementation and it's had memory leaks ever since. This isn't the only context it shows up in. This was reported in the DOTS forum also.
     
    ZenDraL and bazomatic like this.
  8. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
    bazomatic and ZenDraL like this.