Unity-Technologies / barracuda-release

Other
567 stars 77 forks source link

Clean up issue #252

Closed loeffel-io closed 2 years ago

loeffel-io commented 2 years ago

Hello,

i am using the barracuda-2.4 package.

As described here https://docs.unity3d.com/Packages/com.unity.barracuda@2.4/manual/MemoryManagement.html and here https://docs.unity3d.com/Packages/com.unity.barracuda@2.4/manual/TensorHandling.html i am clearing the worker and inputs onDestroy.

                var worker = NetworkModelManager.Instance.GetNetworkModel(GroundDetector.Name).GetWorker();
                _groundDetectorDatasetInputs = _groundDetectorDataset.Inputs(_groundDetectorDataset.RawDataset());
                worker.Execute(_groundDetectorDatasetInputs);

                var output = worker.PeekOutput("output_1");
        public void OnDestroy()
        {
            if (_groundDetectorDatasetInputs != null)
            {
                foreach (var key in _groundDetectorDatasetInputs.Keys)
                {
                    _groundDetectorDatasetInputs[key].Dispose();
                }

                _groundDetectorDatasetInputs.Clear();
            }

            Destroy();
        }
      private void Dispose()
      {
          foreach (var keyValuePair in _networkModels)
          {
              keyValuePair.Value.GetWorker().Dispose();
          }
      }

      private void OnDestroy()
      {
          Dispose();
      }

Everything works as expected as long i am stop the application:

GarbageCollector disposing of ComputeBuffer allocated in C:\Users\lucas\Documents\Unity\MTB Dirt\Library\PackageCache\com.unity.barracuda@2.4.0-preview\Barracuda\Runtime\Core\Backends\BarracudaReferenceCompute.cs at line 73. Please use ComputeBuffer.Release() or .Dispose() to manually release the buffer.
UnityEngine.ComputeBuffer:Finalize ()

Found unreferenced, but undisposed Tensor data which might lead to GPU resource leak: (GPU:#2143661056 (n:1, h:1, w:1, c:1) buffer: UnityEngine.ComputeBuffer created at: )
UnityEngine.Debug:LogWarning (object)
Unity.Barracuda.D:LogWarning (object) (at Library/PackageCache/com.unity.barracuda@2.4.0-preview/Barracuda/Runtime/Core/Internals/Debug.cs:72)
Unity.Barracuda.ComputeTensorData:Finalize () (at Library/PackageCache/com.unity.barracuda@2.4.0-preview/Barracuda/Runtime/Core/Backends/BarracudaReferenceCompute.cs:121)

as there anything i am doing wrong?

thanks

FlorentGuinier commented 2 years ago

Hi Ioeffel,

Seems to me that you are missing a call to Dispose on the worker itself. Please see: https://docs.unity3d.com/Packages/com.unity.barracuda@2.4/manual/MemoryManagement.html

Important note: When calling worker.Destroy() the tensor retrieved here var output = worker.PeekOutput("output_1"); will also be released/dispose. You might want to use .CopyOutput() or .TakeOwnerShip() depending of your use case.

Finally tensor obtained via .PeekOutput(); are only valid until the next call to worker.Execute() call, if you need the data further using .CopyOutput() or .TakeOwnerShip() is also the solution.

Hope it helps :) Florent

FlorentGuinier commented 2 years ago

I'm closing the issue as i feel the question was answered. Please reopen if needed!

loeffel-io commented 2 years ago

Thanks ❤️

The problem were undisposed inputs - made it!