Author Topic: Help understanding memory leaks.  (Read 6117 times)

JosephFerano

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Help understanding memory leaks.
« on: October 14, 2012, 09:47:28 PM »
Hi guys, I'm running this code and apparently, my Menu atlas isn't getting destroyed between scenes. Please forgive me if I come off as a novice in this area, I know very little about memory leaks or general memory management. I know just the basics. So here's what I'm doing;

  1.         IEnumerator TextureCheck() {
  2.                 yield return new WaitForSeconds(2);
  3.                 while (true) {
  4.                         Object[] textures = Resources.FindObjectsOfTypeAll(typeof(Texture));
  5.                         Object[] materials = Resources.FindObjectsOfTypeAll(typeof(Material));
  6.                         Object[] atlas = Resources.FindObjectsOfTypeAll(typeof(GameObject));
  7.                         List<Object> objects = new List<Object>();
  8.                         objects.AddRange(textures);
  9.                         objects.AddRange(materials);
  10.                         objects.AddRange(atlas);
  11.                         foreach (Object menuAtlas in objects) {
  12.                                 if (menuAtlas.name == "Menu") {
  13.                                         print (menu.GetType());
  14.                                 }
  15.                         }
  16.                         textures = null;
  17.                         materials = null;
  18.                         atlas = null;
  19.                         objects = null;
  20.                         yield return new WaitForSeconds(2);
  21.                 }
  22.         }
  23.  

As I move into other levels, this keeps spitting out in the console those 3 objects (The UIAtlas gameObject, the Material, and the Texture). I tried destroying them but I get the "Destroying assets is not permitted" error. This is a massive 8mb texture that is really eating a lot of memory. I really need help in solving this because we need to get this working on the 3GS. Thank you for your help.
« Last Edit: October 14, 2012, 09:49:20 PM by JosephFerano »

JosephFerano

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: Help understanding memory leaks.
« Reply #1 on: October 14, 2012, 09:52:21 PM »
Hopefully this will also help shed some light into the matter. I'm running this in a Buffer scene that I always load between the main menu, cutscenes, and the levels. During this time, I'm just showing a loading screen.

  1.         void Start()
  2.         {
  3.                 System.GC.Collect();
  4.                 Resources.UnloadUnusedAssets();
  5.                 StartCoroutine(WaitToLoadLevel());
  6.         }
  7.  
  8.         IEnumerator WaitToLoadLevel()
  9.         {
  10.                 yield return new WaitForSeconds(2.5f);
  11.                 AsyncOperation load = Application.LoadLevelAsync(LevelLoading.Instance.levelToLoad);
  12.                 LevelLoading.Instance.WaitForLoadDone(load);
  13.         }

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: Help understanding memory leaks.
« Reply #2 on: October 15, 2012, 03:19:28 AM »
Why does it appear that your first segment of code posted up has an infinite While() loop that never exits and loads the resources and tries to add them to your list of objects every two seconds?  Am I missing some blocking case or an exit?

JosephFerano

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: Help understanding memory leaks.
« Reply #3 on: October 15, 2012, 07:52:58 PM »
It's in a gameObject that doesn't get destroyed between levels. So this just keeps running in-between levels. It's just a test block as well, I turn it off during gameplay.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Help understanding memory leaks.
« Reply #4 on: October 16, 2012, 02:18:28 AM »
As long as anything references the asset, it will not get unloaded by Resources.UnloadUnusedAssets()

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: Help understanding memory leaks.
« Reply #5 on: October 16, 2012, 03:00:41 AM »
Well, I'm seriously considering using Resources.UnloadAsset(MY_PARTICULAR_TEXTURE) on a bunch of textures at Runtime, because I want to keep my hierarchy of GameObjects in place but really unload the textures when I don't need them (shop interface for example).

So I'm moving all of my atlases to the Resources folder and will try to do that as clean as possible.

JosephFerano

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: Help understanding memory leaks.
« Reply #6 on: October 16, 2012, 10:34:51 AM »
As long as anything references the asset, it will not get unloaded by Resources.UnloadUnusedAssets()

See, I figured that that was my problem with the NGUI atlases, but how in the world am I supposed to find out what's keeping those references alive? Any tips for that? I guess I can set up some test scenes and see what happens...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Help understanding memory leaks.
« Reply #7 on: October 16, 2012, 10:48:18 AM »
Do you have any widgets in the scene that are referencing them?

JosephFerano

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: Help understanding memory leaks.
« Reply #8 on: October 16, 2012, 11:49:59 AM »
Do you have any widgets in the scene that are referencing them?

I can recheck, now that I think about it, the skip button for the cutscenes might be the culprit. Let me check that right now.

JosephFerano

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: Help understanding memory leaks.
« Reply #9 on: October 16, 2012, 11:54:10 AM »
Answer is negative. Unless I'm missing something. My loading class basically loads a buffer scene, then loads a cutscene, then loads the buffer scene again (thats where the code in my second post is being called), then finally loads the game. I have one GameObject that might be the culprit, and that's my Settings class, which is a singleton and is not destroyed between scenes. I need to try and test that out, maybe that's keeping a reference lingering. However, the odd thing is that all references are destroyed between scenes. So even if in the inspector it says "Missing", would that still keep an atlas from destorying itself, if that "Missing" reference is a gameobject with a class that is inside the atlas hierarchy?