4
\$\begingroup\$

A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details.

In working on saving to file the NativeArrays used to fetch BlendShapeCoefficients in ARFoundation, I encountered the error above.

How does one enable the type of full stack trace this error requires to decipher where things went wrong?

  • I tried the full stack checkboxes on the bottom of Player Settings, but it still does not reveal further details.
  • I have tried to .Dispose() both upon beginning a new recording and on saving. Calling .Dispose led to another error InvalidOperationException: The NativeArray has been deallocated, it is not allowed to access it
  • I also tried using brackets [DeallocateOnJobCompletion] above the NativeArray variable declarations (still occurs)
\$\endgroup\$
3
  • \$\begingroup\$ If you'd like help debugging the code that causes this error, I'd recommend editing your question to include a Minimal Complete Verifiable Example of that code. \$\endgroup\$
    – DMGregory
    Oct 2, 2020 at 8:48
  • 1
    \$\begingroup\$ As for enabling the full stack trace: I'm doing this out of my head, so I might be partially wrong, also this is based on how it worked in Unity 2019, in 2020 some might have moved to project settings by now, not sure. That setting should be under the top bar menu of 'jobs' (Next to File, Edit, Assets, that menu). However, this 'jobs' menu item (or the 'enable full stack trace' sub menu item) doesn't exist without the correct packages. I believe I had to get the 'jobs' package for it to exist, but might even need the 'Entities' package, not sure... \$\endgroup\$
    – troien
    Oct 2, 2020 at 9:23
  • \$\begingroup\$ i wish this were better documented @troien \$\endgroup\$
    – ina
    Oct 2, 2020 at 20:38

4 Answers 4

12
\$\begingroup\$

Unity 2020+, in it's infinite wisdom, removed the ability for you to easily find the cause for this message. They provided a workaround, but as you will see following these steps, it is extremely hacky:

  • Go Window -> Package Manager
  • Click the + icon at the topleft, select add package from git URL
  • Enter com.unity.jobs (It's not a git URL, but that's what Unity wants you to do)

You now have the Jobs tab at the top of your Editor.

  • Go Jobs -> Leak Detection -> Full Stack Traces (Expensive)
  • Close and open the editor again, then rerun the game
\$\endgroup\$
2
\$\begingroup\$

How does one enable the type of full stack trace this error requires to decipher where things went wrong?

Jobs > Leak Detection > Full Stack Traces

When you do this, instead of getting "just" an error that there was a memory leak, it will also tell you on which line the thing that's leaking was allocated. This can help you determine which script is creating the memory leak and which part of that script is leaking.

As for when you should dispose - you do it when you're done with the thing. Part of when you're "done" with it will depend on what Allocator you're using; Temp, Persistent, etc.

enter image description here

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I didn't see that menu in my unity editor. \$\endgroup\$
    – Xwtek
    Jun 4, 2021 at 1:01
  • \$\begingroup\$ @Xwtek - What version of Unity are you using? I posted this on February 23rd, and probably would have been using 2020.1 at the time; I'm using 2021.1 now and it's exactly the same. Also, please verify that you have the Jobs package installed: you have to go to "Window" then "Package Manager" then click the + in the upper-left of the package manager and use "Use Package By Name" then enter the name of the Jobs package com.unity.jobs \$\endgroup\$
    – Chuck
    Jun 4, 2021 at 3:03
  • 1
    \$\begingroup\$ I used unity 2020.3.4f1 and I tried to click the plus sign, but it there is no Use Package By Name. I used Unity Jobs extensively in project, so I'm pretty much sure that I had com.unity.jobs installed. \$\endgroup\$
    – Xwtek
    Jun 4, 2021 at 10:01
2
\$\begingroup\$

in the 2022 version it's moved to the Preferences/Jobs tab Unity Editor, jobs leak detection

\$\endgroup\$
0
\$\begingroup\$

get package com.unity.entities then you can enable the full stack traces option in menu. which will eventually tell you the reason of leakage.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .