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

  1. What happened:
    I upgraded to Unity 2021.3.3f1 LTS and I had the error: “A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details.”
    Searching for the solution it seems that installing “com.jobs.entities” and than check from menu Jobs > Leak Detection > Ful Stack Traces (expensive), should solve the problem. But not for me. After installing entities (it took me hours!! it remained very long “waiting for package to install…”), I didn’t see the option “Leak Detection” in "Jobs. So, I restarted Unity and when I open I don’t see anymore not even “Jobs” this time. And this time show this error I reported here:

Library\PackageCache\com.unity.entities@0.50.1-preview.2\Unity.Entities\RetainBlobAssetSystem.cs(6,5): error SGICE002: Seeing this error indicates a bug in the dots compiler. We’d appreciate a bug report (About->Report a Bug…). Thnx! <3 System.IO.IOException: Cannot create ‘C:\Users\emili\My project\Library\Bee\artifacts\1900b0aE.dag\Unity.Entities.AdditionalFile.txt’ because a file or directory with the same name already exists.

  1. How can we reproduce it using the example you attached:
    I showed out of nothing, just by upgrading to 2021.3.3
    but then I changed my project to 2020, and it shows also there.
    After installing entities, I got a bunch of other errors.

PS: I removed Unity and Unity Hub completely from my pc, and installed it again. The error persists!

2 Answers

2

It happened the same to me and the solution was simpler that it seems, just call Dispose() at the end of your webRequest will fix it:

 private IEnumerator Login(string userId, string password)
                {
                    string page = "login";
                    string apiPath = apiURL + page;
        
                    LoginAccount loginAccount = new LoginAccount(userId, password);
                    string jsonString = JsonUtility.ToJson(loginAccount);
                    byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
        
                    UnityWebRequest webRequest = new(apiPath, "POST");
                    webRequest.uploadHandler = new UploadHandlerRaw(bodyRaw);
                    webRequest.downloadHandler = new DownloadHandlerBuffer();
                    webRequest.SetRequestHeader("Content-Type", "application/json");
        
                    yield return webRequest.SendWebRequest();
        
                    switch (webRequest.result)
                    {
                        case UnityWebRequest.Result.ConnectionError:
                        case UnityWebRequest.Result.DataProcessingError:
                            Debug.LogError(page + ":

Error: " + webRequest.error);
break;
case UnityWebRequest.Result.ProtocolError:
Debug.LogError(page + ":
HTTP Error: " + webRequest.error);
break;
case UnityWebRequest.Result.Success:
Debug.Log(page + ":
Received: " + webRequest.downloadHandler.text);
StartCoroutine(myUser.ProfileInfo(userId));
break;
default:
Debug.Log(page + ":
" + webRequest.result);
break;
}

                    webRequest.Dispose();
                }

This helped in my scenario. I had a lot of UnityWebRequests which were not disposed.

Thank you so much, such a simple thing, but when I see 'Memory leak' I get very intimidated.

2023, still worked like a charm. Thanks.

Yes, thank you!

Worked for me as well. Thank you.

I found what is causing me the memory leak. It was WakaTime plugin. A plugin to monitor the time you spend on using Unity. This is the issue on GitHub: Native Collection has not been disposed, resulting in a memory leak. · Issue #29 · vladfaust/unity-wakatime · GitHub