playMaker

Author Topic: sudden drops(best guess, GC collect or set active)  (Read 6600 times)

muppetpuppet

  • Junior Playmaker
  • **
  • Posts: 77
    • View Profile
sudden drops(best guess, GC collect or set active)
« on: October 13, 2013, 11:53:53 AM »
So I get a single frame performance drop in both editor and my android build.  bout every other 15-20 seconds. 

 when deep profiling I can find that since i'm pooling a lot of stuff that setting active will incur a lot of gcalloc (bout 250k up to 0.6 mb sometimes).

Now i'm not afraid of a little collecting or other overhead, as my nexus 7 is running the game at 100+fps.  but the GCcollect seems to stop the gamethread dead in its tracks, causing a more than noticable hard hickup.    So , and this is my current theory, after activating an object from the pool a lot of GC alloc is added, which is cleaned up bout 20 seconds later. Where the initial setactive is no big bump in the framerate, the GC.collect seems to drop the entire mainthread until it's done. Which provides a very noticable hickup in my otherwise hyper smooth game..

I've tried calling the GC.collect manually, but that gives the same annoying stutter but then more often..   (it doesn't seem the lighten the load of the regular GC)   

So I'm wondering.  a) when using your own pools, is there an alternative to setactive? ,
b)would turning off recursively help,(i have fsm scripts on children as well, but not that knowledgable what effect this might have)
c) is it normal for a setactive to incur a lot of GCalloc?

I've already minimized so much code, used pooling, no textures (the game has zero textures, only a few fonts) etc etc. interlaced every possible script. 

with an end result of a 3d rts that runs 60fps on ipad 2 and 100+fps on nexus 7.  But I'm going mental trying to remove the stutter, I realize there's not much one can do bout theGC.collect.  But any help is very appreciated.

cheers
tomas


« Last Edit: October 14, 2013, 09:51:48 AM by muppetpuppet »

muppetpuppet

  • Junior Playmaker
  • **
  • Posts: 77
    • View Profile
Re: sudden drops(best guess, GC collect or set active)
« Reply #1 on: October 13, 2013, 12:31:34 PM »
Okey I'm trying to remove the set active , recursive , to non recursive.  seems to make a bit of difference.     I'm guessing that that's the reason that the gameObject.active was made obsolete?

Does this make any sense, would be good to know If this is a good direction for optimization, and if so, additonaly any news when some of the playmaker actions will be updated for 4.2?

--update, it does make some difference, but I still get sharp drops regularly. ;(


cheers
tomas
« Last Edit: October 13, 2013, 12:33:32 PM by muppetpuppet »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3201
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: sudden drops(best guess, GC collect or set active)
« Reply #2 on: October 13, 2013, 04:23:58 PM »
Are you using Unity GUI?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12342
  • Official Playmaker Support
    • View Profile
Re: sudden drops(best guess, GC collect or set active)
« Reply #3 on: October 14, 2013, 05:11:02 AM »
Hi,

Do you get the drops exactly when you deactivate elements in your scene? or randomly? In other word, can you relate the Performance drop to an action you do in the game?

The important bit of the GC alloc is the time it takes to find garbage and clear it, Cany ou share these numbers?

The thing is, GC is not automatically done in Unity, you have to call it manually, so I would tend to believe the performance hit may not be due to GC. I am not sure if activating and deactiving object actually call GC,

do you have logics implemented in these object duriong activation and deactivation? mayba that's what causing the drop, you may use a unity function that takes time and you need to refactor this bit then.


 Bye,

 Jean

muppetpuppet

  • Junior Playmaker
  • **
  • Posts: 77
    • View Profile
Re: sudden drops(best guess, GC collect or set active)
« Reply #4 on: October 14, 2013, 09:33:25 AM »
Thanks for the reply, i'll try and answer as best I can

1. I do get drops when I activate , de-activate seems less stressfull.
 I've tried to minimize the amount of activations, But I do need to activate units from the pool , once they're summoned, idem for stuff like resources that you find in the level. 

2. I can setup a dropbox of the project,and PM you the link, if you feel that helps? 
 I can see for instance on activating a unit gameobject that it allocs 0.6 mb , Its hard to figure out where that comes from , animations, scripts , hard to tell

3. I thought GC.collect was not called, but Unity3d had its own garbage collector that runs during the main thread?
 additonally I see some heavy GC.collect calls after for instance a get nearest object by tag call in the profiler, If Unity3d doesn't trigger it, is it somewhere in the playmaker code? (its not in the action), and can I remove the GC.collect from playmaker. (the game's got no textures so should be fine on memory,and I can manually clean it between levels)

4. And indeed I have logics and FSM components that awake on activation, on most objects that get pooled.
can I delay the FSM awake somehow?. Or is this a matter of iterating through an object and manually activating fsm components after the parent is activated?  (that would be sucky and timeconsuming, but if it works , why not)

5. I can life with the drops during object activations, as the camera is usually still when this happens, and the user's attention is focused on the gui.
But its the random drops when nothing much is happening, that is the problem, when units are just moving around, and the camera is following, nothing is spawned or done expect moving stuff around(using A* here, but that barely makes a dent in the profiler)

6. I am using some GUI, not GUIlayout(thats disabled in the playmaker gui object)
I'm using 1 guibox for subtitles, and a bunch of meshtext components , that's it. No textures in the gui except the font, (i've been through the horror of ongui before.)  I could remove the subtitle box, and replace with another meshtext component, but the hickups where there before I implemented the subtitles.
For the rest I have a small fps box for development, which I regularly turn of to compare, doesn't seem to cause hickups..

phew,, lots of info. I hope it makes sense. Just let me know If you are able to look at the project If I share it?  thanks for the quick response, (playmaker is awesome btw, nearly 10 years of Virtools development up my sleeve, and had not programmed in 3 years since our studio converted to unity3d. Playmaker got me right back into it. thanks)

----------notes---

the activation drop, seems to be caused by actionData.CreateAction..   

Could it be that its smarter to activate all the pooled objects once on levelload, and then deactivate them?   It should only need to build the fsm actions once, right? not every time you activate an object?

I'm going to hunt down that gc.collect now.
« Last Edit: October 14, 2013, 09:51:58 AM by muppetpuppet »

muppetpuppet

  • Junior Playmaker
  • **
  • Posts: 77
    • View Profile
Re: sudden drops(best guess, GC collect or set active)
« Reply #5 on: October 14, 2013, 09:47:20 AM »
i've found the GC. Collect , Its not cleaning up much, but its taking 40ms to do so.

here's a link to the profiler

http://i.imgur.com/MucDdKO.jpg


please ignore the argument outofrange error, Its unrelated hashtable error from game startup..
« Last Edit: October 14, 2013, 09:51:05 AM by muppetpuppet »

muppetpuppet

  • Junior Playmaker
  • **
  • Posts: 77
    • View Profile
Re: sudden drops(best guess, GC collect or set active)
« Reply #6 on: October 14, 2013, 10:34:47 AM »
So two problems then
1 . the activation of an game object with FSM attached which takes a lot of overhead to build its actions, 
2. GC.collect triggered somehow in the FSM.log when doing string.concat etc.

Both seem rather fundamental issues, and not easily solvable. Perhaps the GC.collect can be disabled in the playmaker source, but to be honest i 'm not programmer enough to figure that one out.

The activation, perhaps there's another way to activate an fsm and I'm just doing it wrong :). I stayed away from creating gameobjects from prefabs,but rather copying scenobjects to pool,  could that be wrong? On the other hand, it could be i'm confusing the activation drop in the editor with the cgcollect or another hickup in the build. (I'm assuming building the actions is extra heavy in the editor, as its building the visual fsm)

Additionally I could activate all pooled objects from the start and keep them in a dead state, but that seems just wasteful.

« Last Edit: October 14, 2013, 01:35:21 PM by muppetpuppet »

muppetpuppet

  • Junior Playmaker
  • **
  • Posts: 77
    • View Profile
Re: sudden drops(best guess, GC collect or set active)
« Reply #7 on: October 14, 2013, 03:22:55 PM »
OMG, I've removed the single gui box I was using, and it seems to have helped, could it be a single guibox will immediatly trigger all kind of horror.. I knew it was bad , but not this horrible..  I will confirm more.. maybe i'm just to tired

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12342
  • Official Playmaker Support
    • View Profile
Re: sudden drops(best guess, GC collect or set active)
« Reply #8 on: October 14, 2013, 10:15:24 PM »
Hi,

 It is known that Unity GUI system is very bad for performances, you should always use  something like Ngui or 2d toolkit or similar to do your gui if you want something proper. It's more difficult to get in and learn yet again another tool, but this is the only way to go. It's not going to solve all your problem, but definitly improve things dramatically on that front.

bye,

 Jean

muppetpuppet

  • Junior Playmaker
  • **
  • Posts: 77
    • View Profile
Re: sudden drops(best guess, GC collect or set active)
« Reply #9 on: October 15, 2013, 12:31:32 AM »
yeh, well the problem is not so much the performance of the OnGui, but its that it triggers regular Gc.collects.   The game itself ran at 100+ fps on mobile,  but add Ongui element, and the GC.collect creates regular hickups of 60+ms per frame..

The thing is I don't need any gui, everything is 3d, but I needed subtitles..  guess I'll be doing that with meshtext too.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3201
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: sudden drops(best guess, GC collect or set active)
« Reply #10 on: October 15, 2013, 06:11:10 AM »
Note, the FsmLog GC calls shouldn't happen in standalone builds. They are only active in the editor. If you're seeing these in the profiler when connecting to a standalone build, please let me know...

Unity GUI can be pretty inefficient. String operations can also create a lot of garbage. How are you building your subtitle text? Are you building it every frame? Can you build it once when it changes?