Hello Guest

Author Topic: Scene Slows down to a crawl after a while  (Read 9839 times)

vkerkez

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Scene Slows down to a crawl after a while
« on: October 08, 2013, 06:02:30 pm »
Hello,

I am working on a small endless 2d runner. In the background I have 2-3 layers with hills. These hills get created offscreen as a prefab (clone) to the right and then slide in throughout the scene. When they reach a certain point off the screen to the left I call Destroy(this.gameobject);  I can see it being destroyed. Everything looks good. After about 2-3 minutes into the game everything begins to slow down to a crawl on my iphone 5. The game starts rendering slower and slower. My first gut feeling tells me I have a memory leak.  Am I doing something wrong? Is there another way of destroying the objects?

On another note when I create collections and commit my png's I get an error that it will leak memory, but the collection is fine. I assumed this might be just in Unity or is fine.

Thank you for any input. If you require any other information I can provide let me know.


vkerkez

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #1 on: October 08, 2013, 06:05:53 pm »
Should  I be calling System.GC.Collect(); everyonce in a while?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #2 on: October 08, 2013, 06:43:30 pm »
You don't need to call System.GC.COllect, but you should be able to work out why its slowing down by looking in the profiler. Thats the best place to start here.

vkerkez

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #3 on: October 08, 2013, 07:09:33 pm »
I believe the Unity Profiler is only for Pro version. I do not own that. Are there any other tools I could take a look at?

thank you

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #4 on: October 08, 2013, 07:11:09 pm »
I suppose you could look in Unity game view to see how many objects you're drawing? Or the xcode output when running on iOS might give you some info - theres a #define you can turn on somewhere to turn on some additional profiling stuff, it'll tell you a high level overview, nothing specific like the Unity profiler, but still very useful info.

vkerkez

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #5 on: October 08, 2013, 07:13:37 pm »
When I create my sprite collection. I get an a error about it leaking memory when i press commit "This will leak memory" in the console. Is this normal behavior?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #6 on: October 08, 2013, 07:18:11 pm »
What version of tk2d and Unity are you using? And what exactly is the message you're getting?
If its just when you commit a collection you shouldn't have to worry (though this isn't normal or expected behaviour) as it doesn't do this at runtime.

vkerkez

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #7 on: October 08, 2013, 07:25:54 pm »
I can post the exact error message when I get home. I will let you know. I wanted to thank you for fast response time and the great help you are providing. I am very pleased with my purchase. I am sure it has something to do with me, I am making a mistake somewhere.

I will post more info later today.

Thank you again.

vkerkez

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #8 on: October 09, 2013, 03:32:21 am »
Hello here is error i got:

Asset import did not unload metadata path. This will leak memory. File a bug with repro steps please.
UnityEditor.AssetDatabase:ImportAsset(String)
tk2dSpriteCollectionBuilder:SetUpSourceTextureFormats(tk2dSpriteCollection) (at Assets/TK2DROOT/tk2d/Editor/Sprites/tk2dSpriteCollectionBuilder.cs:233)
tk2dSpriteCollectionBuilder:Rebuild(tk2dSpriteCollection) (at Assets/TK2DROOT/tk2d/Editor/Sprites/tk2dSpriteCollectionBuilder.cs:638)
tk2dSpriteCollectionEditorPopup:Commit() (at Assets/TK2DROOT/tk2d/Editor/Sprites/SpriteCollectionEditor/tk2dSpriteCollectionEditorPopup.cs:447)
tk2dSpriteCollectionEditorPopup:DrawToolbar() (at Assets/TK2DROOT/tk2d/Editor/Sprites/SpriteCollectionEditor/tk2dSpriteCollectionEditorPopup.cs:437)
tk2dSpriteCollectionEditorPopup:OnGUI() (at Assets/TK2DROOT/tk2d/Editor/Sprites/SpriteCollectionEditor/tk2dSpriteCollectionEditorPopup.cs:836)
UnityEditor.DockArea:OnGUI()

vkerkez

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #9 on: October 09, 2013, 04:16:58 am »
I checked through and made sure nothing has a reference but the game continues to get slower over time. After 2-3 minutes it begins to get slower and slower.

Not sure what I am doing wrong.

vkerkez

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #10 on: October 09, 2013, 05:19:38 am »
Here is what im kind of doing:

I create random hills ever couple of seconds:
switch(type){
         case 0:
            Instantiate(backHill1, new Vector3(UnityEngine.Random.Range((start + SCREEN_WIDTH/2)-600, (start + SCREEN_WIDTH/2)+600), 380, 9), Quaternion.identity);
            break;
         
         case 1:
            Instantiate(backHill2, new Vector3(UnityEngine.Random.Range((start + SCREEN_WIDTH/2)-600, (start + SCREEN_WIDTH/2)+600), 380, 9), Quaternion.identity);
            break;
         case 2:
            Instantiate(backHill3, new Vector3(start, 380, 9), Quaternion.identity);
            break;
         
         case 3:
            Instantiate(backHill4, new Vector3(start,380, 9), Quaternion.identity);
            break;
         case 4:
            Instantiate(backHill5, new Vector3(start, 380, 9), Quaternion.identity);
            break;
         case 5:
            Instantiate(backHill6, new Vector3(UnityEngine.Random.Range((start + SCREEN_WIDTH/2)-600, (start + SCREEN_WIDTH/2)+600), 380, 9), Quaternion.identity);
            break;
         default:
            break;
         
      }

The object itself has a script  and once the object reaches the end of the screen i am trying:

            Destroy(this.gameObject);
            
            Resources.UnloadUnusedAssets();
            System.GC.Collect();

still the game after 2-3 minutes begins to slow donw

vkerkez

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #11 on: October 09, 2013, 05:41:29 am »
So after investigating it some more.

Here is what the Xcode has to say:

Memory is growing really slowly... So i dont know if this is the real issue.

The Game starts at 30fps and the frame rate starts to go down hill from there as time passes. I am seeing something strange... It seems that the cpu slowly starts to get throttled staring at 34% and 30fps it goes up to 101% and 9fps.  It seems like the gpu is not used at all.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #12 on: October 09, 2013, 09:54:02 am »
I'm not sure what else to suggest, to be honest - this is one of those things that can be tracked down really quickly with the profiler. Perhaps you could get someone with Unity pro to run it for you and find out whats gone wrong?


In any case, surely you don't need to keep instantiating those objects - you could just show, move them to the correct positions and hide them when done? Especially since you've got a reference to them already...

vkerkez

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #13 on: October 09, 2013, 06:20:46 pm »
Again thank you so much. I will let you know if I find a solution.

vkerkez

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Scene Slows down to a crawl after a while
« Reply #14 on: October 10, 2013, 03:40:17 am »
Figured it out.

I got in touch with Unity. They let me have another free trail. I used the profiler and it showed me that my buttons were using a huge cpu percentage. I took a look at my classes and found out in my base class i was doing this:

in my Update(){
      uiItem.OnDown += ButtonDown;
      uiItem.OnUp += ButtonUp;

}

I was basically staking those classes per frame... which ended up throttling the entire cpu. Thank you again for everyone that took the time to read my posts and thank you 2dToolkit for your wonderful support.