Optimization Help Needed

I have included some random samplings from my profiler below. The game runs smooth for the most part but I get an occasional hiccup. I am having a hard time figuring out why the frametime jumps all the way up to 67.8 in the top readout but based on the averages compared to the others you would not expect this.

I do allocate everything up front that is used in the entire game so there is no allocation during game play.

If anyone has a suggestion I would love to hear it. Even if it is something simple I may have overlooked :slight_smile:

----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  8.0   max: 17.1   avg: 12.3
cpu-ogles-drv> min:  1.6   max:  5.7   avg:  2.5
cpu-present>   min:  1.3   max:  8.3   avg:  2.5
frametime>     min: 17.1   max: 67.8   avg: 23.2
draw-call #>   min:   7    max:   7    avg:   7     | batched:     0
tris #>        min:  1012  max:  1012  avg:  1012   | batched:     0
verts #>       min:  2024  max:  2024  avg:  2024   | batched:     0
player-detail> physx:  0.0 animation:  0.0 culling  1.2 skinning:  0.0 batching:  0.0 render:  9.2 fixed-update-count: 0 .. 1
mono-scripts>  update:  1.2   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 1081344 allocated heap: 1171456  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  7.7   max: 16.6   avg: 12.6
cpu-ogles-drv> min:  1.5   max:  4.9   avg:  2.2
cpu-present>   min:  1.2   max: 10.1   avg:  3.0
frametime>     min: 16.8   max: 36.1   avg: 22.4
draw-call #>   min:   7    max:   7    avg:   7     | batched:     0
tris #>        min:  1012  max:  1012  avg:  1012   | batched:     0
verts #>       min:  2024  max:  2024  avg:  2024   | batched:     0
player-detail> physx:  0.0 animation:  0.0 culling  1.5 skinning:  0.0 batching:  0.0 render:  8.7 fixed-update-count: 0 .. 1
mono-scripts>  update:  1.3   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 1081344 allocated heap: 1171456  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  4.8   max: 16.9   avg: 12.0
cpu-ogles-drv> min:  1.5   max:  7.1   avg:  2.3
cpu-present>   min:  1.2   max: 12.7   avg:  2.9
frametime>     min: 17.5   max: 54.7   avg: 22.7
draw-call #>   min:   7    max:   7    avg:   7     | batched:     0
tris #>        min:  1012  max:  1012  avg:  1012   | batched:     0
verts #>       min:  2024  max:  2024  avg:  2024   | batched:     0
player-detail> physx:  0.0 animation:  0.0 culling  1.3 skinning:  0.0 batching:  0.0 render:  9.1 fixed-update-count: 0 .. 1
mono-scripts>  update:  0.8   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 1081344 allocated heap: 1171456  max number of collections: 0 collection total duration:  0.0
----------------------------------------

That sounds like the garbage collector dumping used data. Are you spawning/killing many objects?

From the log provided this does not seem to be happening. Could you show me a segment from when it stutters?

If so, you should try changing the way it dumps garbage. Try dumping more often with this:

if (Time.frameCount % 30 == 0)
{
System.GC.Collect();
}

or try dumping less often (with a bigger allocation)

function Start() {
var tmp = new System.Object[1024];
// make allocations in smaller blocks to avoid them to be treated in a special way, which is designed for large blocks
for (var i : int = 0; i < 1024; i++)
tmp = new byte[1024];

  • // release reference*
    tmp = null;
    }
    Both of those are from the docs. Try reading this page: file:///Applications/Unity%20iPhone/Unity%20iPhone.app/Contents/Documentation/Documentation/Manual/iphone-Optimizing-Scripts.html

thanks for the quick reply jackshiels! My first thought was garbage collection as well; since this caused a similar result during my XNA development time. I have tried both those solutions offered in the documentation without a visible difference.

I am trying to figure out if maybe there are some system events causing it… I know for example when my phone checks for e-mail that there is a visible slowdown while running applications.

There are some built-in features of Unity that seem to do a lot of allocation under some circumstances. The CharacterController is an example - are you using CharacterControllers in your game?

If you can give a few more details about what your game does, it might suggest a few potential hotspots that you can investigate.