I’m creating a simple bullet hell game for Android and I ran into an issue when diagnosing my game using the Memory Profiler where memory usage was increasing after each play-through. I had calls to unload and/or release any assets that were being used but that didn’t seem to make a difference as memory still kept increasing.
Since I couldn’t determine what the exact cause for the leak was, I decided to create a test version of my game on a different branch where everything from the game would be stripped down except for some assets in the gameplay levels.
The main menu uses only Unity’s built in resources and therefore no assets from any asset bundles that need to be downloaded (double-checked groups for addressables just to be sure). It also has 3 buttons that will manually destroy or release any objects that are deemed as addressables or do a Resources.UnloadUnusedAssets() call. Below is a picture of the main menu:
Below is a snapshot of the end of the game after traversing through two game play scenes. Again, there are buttons that will destroy and release any objects that are deemed as addressables or do a Resources.UnloadUnusedAssets()
call.
Here is the snapshot from Unity’s memory profiler. As you can see there is a big difference in Texture2D
which makes sense because the gameplay levels use textures, materials and graphics but the main menu doesn’t.
However, the strange thing happens when I return to the main menu. As you can see in the memory profiler, nothing is being used in terms of Unity Objects. However, Graphics and Untracked memory keep building for some reason. I even pressed the button that calls Resources.UnloadUnusedAssets()
which in theory should unload all the assets I’ve used in the gameplay scenes since they’re unused but it did nothing. In fact, my memory in one gameplay increases 28MB.
Then I decided to do one more play-through and once I returned to the main menu, the memory usage increased another 21MB instead of unloading unused assets from the gameplay scene. Again, nothing unusual when comparing the Unity Objects being used from the screenshot of the main menu scene at the start of the app running vs. the main menu scene after two play-throughs.
However, the untracked and graphics memory keeps increasing for some weird reason and there’s even an error with Graphics memory addresses:
So this means there was a ~47MB increase in memory usage when doing two play-throughs and going back to the main menu which doesn’t make sense if assets are being unloaded because at least we would see a slight decrease in memory usage when returning to the main menu but the memory usage never decreases as it just keeps going up.
I’ve tried unloading assets first in a controlled coroutine before unloading the whole scene using Addressables.UnloadSceneAsync()
. I’ve tried both Addressables.Release()
and Addressables ReleaseHandle()
on objects that are not going to be used anymore. I’ve even tried manually releasing assets through calls like GC.Collect()
and Resources.UnloadUnusedAssets()
after a new scene finished loading or after returning to the main menu but nothing I do reduces memory.
I think I was pretty thorough when writing the code used for managing my app’s memory. What might the issue be in this case? Could there be a chance that the memory readings are giving false information? What are the other possibilities that this memory leak happens? I’ve spent a week dissecting and simplifying my project in order to figure out the problem but to no avail.