I have a question regarding memory release in Unity. Our game uses Unity version 2020.3.33f1, running on the Android platform. There is a UI interface (a prefab) in the game that references many images. Before destroying the interface, we notice an increase in memory usage (an additional 60MB), which is reasonable. However, after destroying the interface and calling Resources.UnloadUnusedAssets
, the memory usage decreases by only 20MB, which is much less than the increase. From the memory profiler, it seems that resources like textures have been released, but most of the memory reduction is offset by an increase in ‘Untracked Memory’.
My question is, what could be causing this phenomenon? Is there a way to properly reduce the newly allocated memory (especially the memory occupied by textures)?
Hello,
First, 2020.3 has more things that are untracked than 2022.3 or newer, among these IL2CPP VM Memory and some graphics resource sizes are wrongly calculated & reported, namely RenderTextures, 2D/3DCubeMap Arrays and some I might be forgettting.
Second, Graphics is the estimated amount of RAM(*) used by graphics resources within the Graphics Driver / API (Vulkan, DX11, OpenGL, Metal, etc). Unity tells the Gfx Driver what it needs and what for, but does not control that memory, has no access to it and doesn’t know where the allocations are gonna be made. Unity’s native code just precalculates (based on the parameters and knowing that the driver is gonna do with it) the size and keeps track of it via a shadow copy, until it instructs the driver that it is no longer needed.
What the driver does with that memory afterwards is up to the driver. It might keep it or the memory it allocated for it around as reserved. From the prespective of the Memory Profiler, it returns to being Untracked memory. You’ll need to use native platform tooling like Android Studio’s profiler to get more insights into Untracked memory if that’s important to know for you.
(* Android usually has unified memory and no dedicated VRAM, but on platforms/devices with dedicated VRAM, the driver usually keeps a RAM copy in case VRAM gets flushed out to display a different app, or if the GPU decides it didn’t touch some memory for a moment and now needs it for something else. None of the Memory captured by the Memory Profiler is VRAM, though the Graphics amount probably aligns pretty closely to it.)
1 Like