Here's the error message:
A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details.
I get it on a regular basis, sometimes when the code is running, sometimes when I'm just in the editor. It doesn't seem to be breaking anything but I'd like to be able to fix it.
Answer by Ivan Cai · Apr 28, 2021 at 09:24 AM
This issue seems to belong to the business category of unity, not playfab. You need to navigate to [Jobs]->[Leak Detection]->[Full Stack Traces], select it and restart unity.Then the error content in the console will be show that the memory leak occurred in the data retention location. After knowing the location of the leak, you just put the variable in Dispose, but it is recommended to enclose it in the using statement.
Answer by Ralv Wohlgethan · May 10, 2021 at 01:25 AM
@Richard Berger You might need to actually include the jobs package (manually, maybe) in your manifest.json file:
"dependencies": { ... "com.unity.jobs": "0.8.0-preview.23", ... }
Depending on who reads this post and when, the actual package version may of course differ, but you should be able to update from the editor (PackageManager UI) after modifying your manifest.json (unless Unity decides to drop the package or integrate it elsewhere, of course, but then this answer will be outdated anyway...).
Answer by Ralv Wohlgethan · May 10, 2021 at 01:25 AM
Actually (if it gets published) you can ignore my previous answer. While the number of memory leaks has seemingly decreased with the integration of the latest jobs package, they do still occur and seem to originate from the PlayFabUnityHttp class. At least this is what I'd conclude from the stacktrace:
A Native Collection has not been disposed, resulting in a memory leak. Allocated from: Unity.Collections.NativeArray`1:.ctor(Byte[], Allocator) (at /Users/bokken/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:69) UnityEngine.Networking.UploadHandlerRaw:.ctor(Byte[]) (at /Users/bokken/buildslave/unity/build/Modules/UnityWebRequest/Public/UploadHandler/UploadHandler.bindings.cs:98) PlayFab.Internal.<Post>d__12:MoveNext() (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:129) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutineManaged2(IEnumerator) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/MonoBehaviour.bindings.cs:91) PlayFab.Internal.PlayFabUnityHttp:MakeApiCall(Object) (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:119) PlayFab.Internal.PlayFabHttp:_MakeApiCall(String, String, PlayFabRequestCommon, AuthType, Action`1, Action`1, Object, Dictionary`2, Boolean, PlayFabAuthenticationContext, PlayFabApiSettings, IPlayFabInstanceApi) (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabHTTP.cs:228) PlayFab.Internal.PlayFabHttp:MakeApiCall(String, PlayFabRequestCommon, AuthType, Action`1, Action`1, Object, Dictionary`2, PlayFabAuthenticationContext, PlayFabApiSettings, IPlayFabInstanceApi) (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabHTTP.cs:133) PlayFab.PlayFabClientAPI:ReportDeviceInfo(DeviceInfoRequest, Action`1, Action`1, Object, Dictionary`2) (at Assets/PlayFabSDK/Client/PlayFabClientAPI.cs:1751) PlayFab.Internal.PlayFabDeviceUtil:SendDeviceInfoToPlayFab(PlayFabApiSettings, IPlayFabInstanceApi) (at Assets/PlayFabSDK/Client/PlayFabDeviceUtil.cs:54) PlayFab.Internal.PlayFabDeviceUtil:_OnPlayFabLogin(UserSettings, String, String, String, PlayFabApiSettings, IPlayFabInstanceApi) (at Assets/PlayFabSDK/Client/PlayFabDeviceUtil.cs:127) PlayFab.Internal.PlayFabDeviceUtil:OnPlayFabLogin(PlayFabResultCommon, PlayFabApiSettings, IPlayFabInstanceApi) (at Assets/PlayFabSDK/Client/PlayFabDeviceUtil.cs:102) PlayFab.Internal.PlayFabUnityHttp:OnResponse(String, CallRequestContainer) (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:234) PlayFab.Internal.<Post>d__12:MoveNext() (at Assets/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:198) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)
Don't mind the "/Users/bokken/..." paths, this is Unity's build machine, so no need to worry.
In our own implementations with UnityWebRequest we always use "using" blocks. This ensures that the request is always disposed of properly. I'm not sure why this isn't the case in PlayFabUnityHttp, but a first look seems to imply that it is indeed possible to create a memory leak when the manual call of
www.Dispose()
at the very end isn't reached... Interesting enough, the code contradicts @Ivan Cai's proposal of doing exactly this (using "using" blocks)...
Another error source seems to be in PlayFabEditorHttp:
A Native Collection has not been disposed, resulting in a memory leak. Allocated from: Unity.Collections.NativeArray`1:.ctor(Byte[], Allocator) (at /Users/bokken/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:69) UnityEngine.Networking.UploadHandlerRaw:.ctor(Byte[]) (at /Users/bokken/buildslave/unity/build/Modules/UnityWebRequest/Public/UploadHandler/UploadHandler.bindings.cs:98) PlayFab.PfEditor.PlayFabEditorHttp:MakeApiCall(String, String, GetStudiosRequest, Action`1, Action`1) (at Assets/PlayFabEditorExtensions/Editor/Scripts/PlayFabEditorSDK/PlayFabEditorHttp.cs:74) PlayFab.PfEditor.PlayFabEditorApi:GetStudios(GetStudiosRequest, Action`1, Action`1) (at Assets/PlayFabEditorExtensions/Editor/Scripts/PlayFabEditorSDK/PlayFabEditorApi.cs:30) PlayFab.PfEditor.PlayFabEditorDataService:RefreshStudiosList(Boolean) (at Assets/PlayFabEditorExtensions/Editor/Scripts/Utils/PlayFabEditorDataService.cs:198) PlayFab.PfEditor.PlayFabEditor:OnEnable() (at Assets/PlayFabEditorExtensions/Editor/PlayFabEditor.cs:45) PlayFab.PfEditor.PlayFabEditor:OnFocus() (at Assets/PlayFabEditorExtensions/Editor/PlayFabEditor.cs:62) UnityEditor.HostView:RegisterSelectedPane(Boolean) (at /Users/bokken/buildslave/unity/build/Editor/Mono/HostView.cs:502) UnityEditor.HostView:OnEnable() (at /Users/bokken/buildslave/unity/build/Editor/Mono/HostView.cs:154) UnityEditor.DockArea:OnEnable() (at /Users/bokken/buildslave/unity/build/Editor/Mono/GUI/DockArea.cs:174)
In this case, at first glance, I can't find neither a using block nor any Dispose() call. Hope this helps...
Playfab SDK version: 2.107.210427(shown as latest release as of 2021-05-08)
Answer by Richard Berger · May 27, 2021 at 01:48 AM
Here's the stack trace that I did manage to then get - maybe this helps...
A Native Collection has not been disposed, resulting in a memory leak. Allocated from: Unity.Collections.NativeArray`1:.ctor(Byte[], Allocator) UnityEngine.Networking.UploadHandlerRaw:.ctor(Byte[]) PlayFab.Internal.<Post>d__12:MoveNext() (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabUnityHttp.cs:129) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) UnityEngine.MonoBehaviour:StartCoroutineManaged2(IEnumerator) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) PlayFab.Internal.PlayFabUnityHttp:MakeApiCall(Object) (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabUnityHttp.cs:119) PlayFab.Internal.PlayFabHttp:_MakeApiCall(String, String, PlayFabRequestCommon, AuthType, Action`1, Action`1, Object, Dictionary`2, Boolean, PlayFabAuthenticationContext, PlayFabApiSettings, IPlayFabInstanceApi) (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabHTTP.cs:228) PlayFab.Internal.PlayFabHttp:MakeApiCall(String, PlayFabRequestCommon, AuthType, Action`1, Action`1, Object, Dictionary`2, PlayFabAuthenticationContext, PlayFabApiSettings, IPlayFabInstanceApi) (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabHTTP.cs:133) PlayFab.PlayFabEventsInstanceAPI:WriteEvents(WriteEventsRequest, Action`1, Action`1, Object, Dictionary`2) (at Assets\PlayFabSDK\Events\PlayFabEventsInstanceAPI.cs:63) PlayFab.Public.ScreenTimeTracker:Send() (at Assets\PlayFabSDK\Entity\ScreenTimeTracker.cs:181) PlayFab.Public.ScreenTimeTracker:OnApplicationFocus(Boolean) (at Assets\PlayFabSDK\Entity\ScreenTimeTracker.cs:156) PlayFab.Internal.PlayFabHttp:OnApplicationFocus(Boolean) (at Assets\PlayFabSDK\Shared\Internal\PlayFabHttp\PlayFabHTTP.cs:332) UnityEditorInternal.InternalEditorUtility:OnGameViewFocus(Boolean) UnityEditor.PlayModeView:SetFocus(Boolean) UnityEditor.GameView:OnLostFocus() UnityEditor.HostView:OnLostFocus()
Hi, we have noticed the memory leak issue in 2021 Unity Engine. However, to manually fix this issue, it may need plenty of work to put web request to a using construction, according to: Need help on "A Native Collection has not been disposed, resulting in a memory leak" - Unity Answers.
For now, you can switch to the LTS version of Unity to avoid this warning, otherwise, please wait for the updates of Unity or PlayFab Unity SDK.
Answer by CHO YOUNGMIN · Feb 11 at 02:16 AM
Actually it's easy to solve.
This is a PlayFabSharedSettings ' directory problem.
When you import Playfab SDK, basically PlayFabSharedSettings is exist in [PlayFabSDK] - [Shared] - [Public] - [Resources].
But take a look the PlayFabSettings.GetSharedSettingsObjectPrivate() .
It load all PlayFabSharedSettings from "Resources" like :
var settingsList = Resources.LoadAll<PlayFabSharedSettings>("PlayFabSharedSettings");
return settingsList[0];.
So basically as all we know, Resources. ~~ access to [Asset] - [Resources].
Not [PlayFabSDK] - [Shared] - [Public] - [Resources].
So go [PlayFabSDK] - [Shared] - [Public] - [Resources], Copy PlayFabSharedSettings,
Paste to [Asset] - [Resources]. then it will works.
Thank you for this. This fixed the problem for me and only took a moment to do.
Unity Android Plugin 1 Answer
Can't Install PlayFab Unity SDK. 1 Answer
Unity Editor Extension does not appear 0 Answers
Unity SDK il2cpp compilation issue 2 Answers