Memory grows every scene and can't be cleared

When profiling the app in unity I get an ongoing 4mb memory jump every time I load a scene and come back to the menu. Using System.GC.Collect(), Resources.UnloadUnusedAssets() or changing scenes does not clear this memory either. If you continue using the app the memory will continue to grow and will crash although it never seems to reach the memory roof. I’m also testing on a Samsung Ativ which works perfectly and doesn’t receive this error or the memory jump. This is happening on a Lumia 520 and I receive this corresponding error message in Visual Studio

allocation 0x00000000 already registered @ C:/BuildAgent/work/d3d49558e4d408f4/Runtime/GfxDevice/d3d11/TexturesD3D11.cpp:l445 size 4194304; now calling from C:/BuildAgent/work/d3d49558e4d408f4/Runtime/GfxDevice/d3d11/TexturesD3D11.cpp:l445 size 4194304?

d3d11: failed to create 2D texture id=813 w=2048 h=2048 mips=1 d3dfmt=28 [8007000e]

d3d11: failed to create 2D texture view id=813 [80070057]

It appears as though it is trying to create a new texture and failing but I can’t find a missing texture. I don’t even use 2048 textures which the log suggests it’s trying to create and when this first occurs the memory(Used Total) readout from the profiler reports only around 43.5 mb. Below is the larger segment of the log. If anyone has experienced this or has any idea of whats happening, help would be much appreciated.

Notes: I’m using Unity 4.3.3.

UnloadTime: 3.451703 ms

Unloading 9 Unused Serialized files (Serialized files now loaded: 0 / Dirty serialized files: 0)


Unloading 13 unused Assets to reduce memory usage. Loaded Objects now: 1329. Operation took 301.567566 ms.

System memory in use: 22.3 MB.

UnloadTime: 16.944887 ms

Unloading 1 Unused Serialized files (Serialized files now loaded: 0 / Dirty serialized files: 0)


Unloading 75 unused Assets to reduce memory usage. Loaded Objects now: 938. Operation took 362.374207 ms.

System memory in use: 21.8 MB.

Unloading 0 Unused Serialized files (Serialized files now loaded: 0 / Dirty serialized files: 0)


Unloading 73 unused Assets to reduce memory usage. Loaded Objects now: 865. Operation took 334.938232 ms.

System memory in use: 20.9 MB.

About To Load
 
(Filename: C:/BuildAgent/work/d3d49558e4d408f4/artifacts/WP8SupportGenerated/UnityEngineDebug.cpp Line: 55)


allocation 0x00000000 already registered @ C:/BuildAgent/work/d3d49558e4d408f4/Runtime/GfxDevice/d3d11/TexturesD3D11.cpp:l445 size 4194304; now calling from C:/BuildAgent/work/d3d49558e4d408f4/Runtime/GfxDevice/d3d11/TexturesD3D11.cpp:l445 size 4194304?
 
(Filename: C:/BuildAgent/work/d3d49558e4d408f4/Runtime/Allocator/MemoryManager.cpp Line: 1018)


d3d11: failed to create 2D texture id=813 w=2048 h=2048 mips=1 d3dfmt=28 [8007000e]

d3d11: failed to create 2D texture view id=813 [80070057]

UnloadTime: 3.958962 ms

Unloading 2 Unused Serialized files (Serialized files now loaded: 0 / Dirty serialized files: 0)


Unloading 14 unused Assets to reduce memory usage. Loaded Objects now: 3946. Operation took 386.829773 ms.

System memory in use: 22.3 MB.

The thread 0x330 has exited with code 259 (0x103).
The thread 0x188 has exited with code 259 (0x103).
The thread 0xd34 has exited with code 259 (0x103).
Could not allocate memory: System out of memory!
Trying to allocate: 16520B with 4 alignment. MemoryLabel: DynamicArray
Allocation happend at: Line:333 in C:\BuildAgent\work\d3d49558e4d408f4\Runtime/Utilities/dynamic_array.h
Memory overview


[ ALLOC_DEFAULT ] used: 10338799B | peak: 26346600B | reserved: 15387782B 

[ ALLOC_GAMEOBJECT ] used: 710707B | peak: 734874B | reserved: 862668B 

[ ALLOC_GFX ] used: 44125B | peak: 19598121B | reserved: 45022B 

[ ALLOC_PROFILER ] used: 12912916B | peak: 12912924B | reserved: 13016002B 

Could not allocate memory: System out of memory!
Trying to allocate: 16520B with 4 alignment. MemoryLabel: DynamicArray
Allocation happend at: Line:333 in C:\BuildAgent\work\d3d49558e4d408f4\Runtime/Utilities/dynamic_array.h
Memory overview


[ ALLOC_DEFAULT ] used: 10338799B | peak: 26346600B | reserved: 15387782B 

[ ALLOC_GAMEOBJECT ] used: 710707B | peak: 734874B | reserved: 862668B 

[ ALLOC_GFX ] used: 44125B | peak: 19598121B | reserved: 45022B 

[ ALLOC_PROFILER ] used: 12912916B | peak: 12912924B | reserved: 13016002B 

 
(Filename: C:/BuildAgent/work/d3d49558e4d408f4/Runtime/Allocator/MemoryManager.cpp Line: 793)


The program '[4008] TaskHost.exe' has exited with code -2147483645 (0x80000003).

Hello,

could you report it as a bug with a repro project attached?

Hello again,

It’s quite a large project and I’m not sure what’s causing it but if I can recreate it in a new project I’ll report and upload it.

Alright.

Fixed it in the end by cutting down on memory by a couple more MBs. Still not entirely sure why it was necessary but it hovers on 38.5Mb now according to the profiler and there is no increase or error when traversing scenes.

We are also precisely getting the same error and using GC.Collect() or Resources.UnloadUnusedAssets() or adding blank transition scenes in between also does not help.
Its happening only on WVGA 512mb ( high mem increased to 180mb and testing on the emulator ) and the WVGA only mode runs fine.
As a fix, we are doing a deferred loading of all gameobjects now when its
required ( earlier we were creating all assets at once in Start() when the scene was loaded )
However, its strange that no amount of freeing up from our end works ( i.e GC.Collect() , UnloadUnusedAssets() or blank scenes ):face_with_spiral_eyes:

So I’ve been experiencing a similar issue. Memory is growing after each scene load, and doesn’t come down regardless of requesting a garbage collection or Resources.UnloadUnusedAssets().

Full disclosure, there are a few objects that I need to do Object.DontDestroyOnLoad, and certain scenes I load additively (though outside of a few very light objects, everything else is in the scene gets destroyed). So in these instances, I expect some memory to hang around.

However, before I return to the main menu scene, I make sure to destroy all game objects, even those previously marked as DontDestroyOnLoad, and I load the main menu scene with LoadLevel. Shouldn’t this ensure that objects are reclaimed and memory is freed? I’m basically wiping out all references at this point.

Unfortunately, I don’t have the luxury of using a pro version of Unity right now, so I don’t have access to the profiler, so I’m sort of chasing shadows :\

Any thoughts or ideas on how to possibly identify this (other than getting a pro license :p) is greatly appreciated.

You could use the Visual Studio profiler to see what’s using up the memory. That won’t give you an idea on gameobjects, as most of their memory usage is on native side, but it might still help you identify what’s causing the memory jump).

I’m not sure how I could use the Visual Studio profiler to see memory usage since I think it’s for .NET applications only? I gave it a go and the profile said no data was collected. I was able to profile CPU usage though.

This may not be useful to any of you, but I previously had issues like this, and it was related to my game calling back into code in my Visual Studio solution. I don’t know what I was doing wrong (I simply reworked my game to not call Visual Studio code any more), but as soon as I kept all my code in Unity the memory increase stopped.

Did you by any chance have any static events hooked up? Those are notorious for causing memory “leaks” - as they don’t let the garbage collector collect the gameObjects.

I believe I did use static methods, so your explanation makes sense.

Sorry, but I’m not sure I understand what you mean by this. Could you explain this further?

Having code in my Visual Studio solution (but not in the Unity project), which I then call from within a Unity script. If you look up the Windows porting/getting started PDFs elsewhere on this site you’ll see what it is I’m doing. You create a class in Unity that is effectively an interface for the methods in a Visual Studio class, so your Unity script calls a delegate, which executes the corresponding method in Visual Studio.