Lag spikes every 20sec

Hey. I have new problem with spikes…
Every 20sec i have huge spike what can observe in profiler…

Without spike profiler have something like this:

iPhone Unity internal profiler stats:

cpu-player>    min:  1.2   max: 14.3   avg:  8.3
cpu-ogles-drv> min:  2.8   max:  8.7   avg:  4.9
cpu-present>   min:  1.6   max: 15.8   avg:  4.5
frametime>     min: 16.3   max: 31.3   avg: 23.1
draw-call #>   min:   8    max:   8    avg:   8     | batched:     4
tris #>        min:  5473  max:  5473  avg:  5473   | batched:   140
verts #>       min:  3267  max:  3267  avg:  3267   | batched:    88
player-detail> physx:  0.7 animation:  0.7 culling  0.4 skinning:  0.9 batching:  0.1 render:  4.1 fixed-update-count: 0 .. 1
mono-scripts>  update:  0.8   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 188416 allocated heap: 196608  max number of collections: 0 collection total duration:  0.0

But when i see spike

iPhone Unity internal profiler stats:
cpu-player>    min:  1.1   max: 147.3   avg: 13.6
cpu-ogles-drv> min:  3.0   max:  8.6   avg:  5.0
cpu-present>   min:  1.5   max: 17.1   avg:  5.0
frametime>     min: 16.3   max: 158.8   avg: 28.3
draw-call #>   min:   8    max:   9    avg:   8     | batched:     5
tris #>        min:  5473  max:  5541  avg:  5534   | batched:   201
verts #>       min:  3267  max:  3315  avg:  3310   | batched:   131
player-detail> physx:  0.6 animation:  0.8 culling  0.3 skinning:  1.0 batching:  0.1 render:  4.3 fixed-update-count: 0 .. 1
mono-scripts>  update:  5.6   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 192512 allocated heap: 196608  max number of collections: 0 collection total duration:  0.0

If you see cpu player max grow up from 14 to 150 and every 20sec i have that same problem…

For better performance i change:

Fixed timestep = 0.1
Maximum allowed timestep = 0.25

from AppController.mm
#define FALLBACK_LOOP_TYPE NSTIMER_BASED_LOOP
#define kFPS 60.0
#define kAccelerometerFrequency 1.0

I use only one script for moving character and Joystick script from Penelope

Script for moving:

function Update()
{
     if(!pause)
     {
       Movement();
     }
}

function Movement()
{
		//print(moveJoystick.pixelInset.x + "  " + moveJoystick.pixelInset.y );
		//var AbsX = Mathf.Abs(moveJoystick.pixelInset.x);
	//	var AbsY = Mathf.Abs(moveJoystick.pixelInset.y);
		if(moveJoystickGO.moved)
		{
			transform.rotation = Quaternion.LookRotation(Vector3( moveJoystick.pixelInset.x, 0, moveJoystick.pixelInset.y ));
			speed = 5;
			animation.CrossFade("run");
		}
		else 
		{
			animation.CrossFade("idle");
			speed = 0;
		}
		var moveDirection = transform.TransformDirection(Vector3.forward);
		controller.Move(moveDirection * speed * Time.deltaTime);
		TargetCamera.position = Vector3(thisTransform.position.x, thisTransform.position.y + 5, thisTransform.position.z - 2);
}

Anyone can told me what causes this spikes?

On thing that comes to mind is GUI. If you’re using GUI elements on iPhone this will cause lots of garbage collection which can cause those CPU spikes. I eliminated all my issues by moving all my GUI to using GUITexture’s

Now i use only one GUITexture… don’t use any other GUI elements…

Sounds like garbage collection to me, your scripts may be allocating memory every frame, which can lead to frequent GC hiccups. The line you want to watch in the profiler is this part…

mono-memory> used heap: 192512 allocated heap: 196608 max number of collections: 0 collection total duration: 0.0

GC kicks in when that used heap approaches the allocated heap - the used value will shrink. Also, is your used heap value increasing every frame all the time? This would indicate that your scripts are allocating memory every frame, probably needlessly.