0
\$\begingroup\$

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:

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.

Game End

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. enter image description here

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. enter image description here enter image description here

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. enter image description here

However, the untracked and graphics memory keeps increasing for some weird reason and there's even an error with Graphics memory addresses: enter image description here

enter image description here

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.

\$\endgroup\$
4
  • \$\begingroup\$ Do you use object pooling in your bullet game and might have a mistake in the implementation there/ keeping the objects alive and just increase the overall poolcount? \$\endgroup\$
    – Zibelas
    Commented Jan 28 at 19:30
  • \$\begingroup\$ @Zibelas I tried checking that by creating a test branch of my project and deleting every GameObject from every scene that was being loaded except for the main menu and the persistent player GameObjects and nothing left was responsible for spawning anything. Even after doing this, the problem still persisted. \$\endgroup\$
    – hoffer
    Commented Jan 29 at 7:48
  • \$\begingroup\$ I dont think deleting gameobjects from the scene will free the memory of them as well. It might be helpful to show the actual code that loads and creates your actual bullets with the behind object pool. We can only guess without actually seeing how you do it. \$\endgroup\$
    – Zibelas
    Commented Jan 29 at 11:25
  • \$\begingroup\$ @Zibelas What I meant was that all of the GameObjects in each scene except for the buttons used to traverse between scenes were deleted. My object pooling code or whatever generates my enemies or projectiles etc. are not even in the equation as I deleted everything in each scene in order to diagnose the problem. I have virtually nothing in my scenes the moment I start the app but yet it still shows an increase in memory usage as I mentioned above. \$\endgroup\$
    – hoffer
    Commented Jan 30 at 20:43

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.