Hello Guest

Author Topic: Memory Leak (inconsistent), would love some help!  (Read 8882 times)

39thstreet

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 77
    • View Profile
Memory Leak (inconsistent), would love some help!
« on: October 02, 2012, 07:24:36 pm »

Having a real beast of an issue with my game.  It works 99% of the time (like, seriously, I can play for hours and hours with no issues).   Yet on a handful of occasions I've had a weird error pop up, it's happened on both my platforms (Android and iOS), so it does not appear to be platform specific.

The Game Structure
Our game has 6 scenes that are loaded and unloaded in a semi-random order. When transitioning between a transition screen covers the game (this is a persistent GameObject with a DontDestroyOnLoad). This transition destroys all the elements of the previous scene, then calls the following inside a Coroutine:
Code: [Select]
AsyncOperation asyncunload = Resources.UnloadUnusedAssets()
yield return asyncunload;
async = Application.LoadLevelAsync(nextId);
Once that async is done, the transition hides itself and the next scene is played.

The Problem
Very rarely (as mentioned, has happened maybe 8 or 9 times out of hundreds and hundreds of scene transitions), the transition animation will slow to a crawl.   The transition normally takes 2-3 seconds, but once the bug hits it will take 10-20 seconds.   Once the transition is done the game seems to play perfectly fine (crazy, right?), but when the next transition comes up the slowness will return.

Memory Checks
I added a TON of debug code to try and isolate the issue.  This has revealed the following:
-We haven't been able to pin down the problem as associated with any particular one of our scenes, although it may be.
-There appears to be a weird memory leak.  When the game is working normally, right after the UnloadUnusedAssets() we get a System Memory in Use: 3.0mb -- again it will show right around 3mb every time after every game for hundreds of plays.   

When the game starts messing up, this System Memory in Use goes up!  e.g. System Memory in Use: 17.0mb or higher.   In other words, it sure looks like memory is not being cleared.  Sometimes it will fall back down to 3.0 after the bad transition, other times it will stay at this high level through more transitions. 

The really bad part is when it sticks like that, because the game appears to be totally broken (20 second transitions every time), and the only way to fix it is to force quit the app (something many Android and iOS users have no idea how to do).

That's about all the information I have so far, any advice on what this might be or ideas on how to isolate the issue would be immensely helpful.  We were hoping to launch this week  :-\



unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Memory Leak (inconsistent), would love some help!
« Reply #1 on: October 02, 2012, 07:47:24 pm »
I do have some suggestions from debugging some other non tk2d related projects. I have found that Unity does some pretty bizzare stuff sometimes with memory management. First step is to find out whats being left over.

Before a transition and right after unloading assets log the following:

GC.GetTotalMemory()
Resources.FindObjectsofTypeAll(Texture) and (Material) and (SpriteCollection). Log by name and instanceId. You could also log EVERYTHING, that might help too, but will be big.

Now you should (hopefully) have a good idea of what's getting left over. Once you have a list of these you can do some analysis of what's getting left behind.

39thstreet

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 77
    • View Profile
Re: Memory Leak (inconsistent), would love some help!
« Reply #2 on: October 02, 2012, 07:51:27 pm »
Thanks, that is helpful!  I'll do some more testing.

I saw a mention of mesh memory leaks in the latest 1.8 alpha notes, this couldn't be related to that could it?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Memory Leak (inconsistent), would love some help!
« Reply #3 on: October 02, 2012, 08:02:50 pm »
Very unlikely, but theres a chance it could be confusing Unity's asset management and not getting unloaded properly.
If you wan't you can patch those changes in to yours. Its just a few lines.

In tk2dSprite.cs & tk2dTextMesh.cs, look for lines which say
Code: [Select]
Mesh mesh = new Mesh();
and add:
Code: [Select]
mesh.hideFlags = HideFlags.DontSave;
right after that.

After that, resave all your scenes and you should be good to go.