The 3D game I’m developing has a significant lag every 10 seconds or so on Android platform. I profiled the application with Logcat and built-in profiler and found following messages when the lag occurs.
D/Unity (13042): Android Unity internal profiler stats:
D/Unity (13042): cpu-player> min: 3.4 max: 11.5 avg: 5.7
D/Unity (13042): cpu-ogles-drv> min: 1.3 max: 8.5 avg: 2.4
D/Unity (13042): cpu-present> min: 1.1 max: 23.5 avg: 9.8
D/Unity (13042): frametime> min: 10.1 max: 29.9 avg: 17.9
D/Unity (13042): draw-call #> min: 22 max: 22 avg: 22 | batched: 111
D/Unity (13042): tris #> min: 4947 max: 4983 avg: 4965 | batched: 5769
D/Unity (13042): verts #> min: 5141 max: 5203 avg: 5173 | batched: 3442
D/Unity (13042): player-detail> physx: 0.1 animation: 0.3 culling 0.0 skinning: 0.4 batching: 0.5 render: 3.4 fixed-update-count: 0 .. 1
D/Unity (13042): mono-scripts> update: 0.7 fixedUpdate: 0.0 coroutines: 0.0
D/Unity (13042): mono-memory> used heap: 520192 allocated heap: 643072 max number of collections: 0 collection total duration: 0.0
D/Unity (13042): ----------------------------------------
D/PhoneWindow(13042): couldn't save which view has focus because the focused view com.unity3d.player.UnityPlayer$13@40540ac0 has no id.
D/Unity (13042): NativeActivity save instance state: 0x29d5b0
D/Unity (13042): NativeActivity pause: 0x29d5b0
I/ActivityManager( 128): Starting: Intent { flg=0x10000000 cmp=com.google.android.apps.maps/com.google.googlenav.login.AndroidLoginActivitySdk5 (has extras) } from pid 11945
I/GLThread(13042): onPause tid=11
I/Main thread(13042): onPause waiting for mPaused.
V/AudioPolicyManager( 83): stopOutput() output 1, stream 3, session 960
V/AudioPolicyManager( 83): getNewDevice() selected device 0
V/AudioPolicyManager( 83): setOutputDevice() output 1 device 0 delayMs 0 force 0
V/AudioPolicyManager( 83): setOutputDevice() setting same device 0 or null device for output 1
I/GLThread(13042): mPaused is now true tid=11
I/GLThread(13042): releasing EGL surface because paused tid=11
W/EglHelper(13042): destroySurface() tid=11
W/EglHelper(13042): finish() tid=11
I/GLThread(13042): releasing EGL context because paused tid=11
E/SensorManager(13042): unregisterListener:: all sensors, listener = com.unity3d.player.UnityPlayer@4052f380
W/ActivityManager( 128): Trying to launch com.google.android.apps.maps/com.google.googlenav.login.AndroidLoginActivitySdk5
D/Unity (13042): NativeActivity window focus changed: 0x29d5b0 -- 0
D/Unity (13042): NativeActivity window focus changed: 0x29d5b0 -- 1
D/Unity (13042): NativeActivity resume: 0x29d5b0
W/InputManagerService( 128): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40aa8b00
I/InputReader( 128): dispatchTouch::touch event's action is 0
I/InputDispatcher( 128): Delivering touch to current input target: action: 0, channel '4096d230 uk.co.nextgen.towerdefense/com.unity3d.player.UnityPlayerNativeActivity (server)'
I/InputReader( 128): dispatchTouch::touch event's action is 1
I/InputDispatcher( 128): Delivering touch to current input target: action: 1, channel '4096d230 uk.co.nextgen.towerdefense/com.unity3d.player.UnityPlayerNativeActivity (server)'
D/InfoAlarmWidgetNews(12435): onReceive
D/InfoAlarmWidgetNews(12435): widget onReceive ->InfoAlarmWidget.action.widget.news.scroll
I/GLThread(13042): onResume tid=11
I/Main thread(13042): onResume waiting for !mPaused.
I/GLThread(13042): mPaused is now false tid=11
W/EglHelper(13042): start() tid=11
V/AudioPolicyManager( 83): startOutput() output 1, stream 3, session 961
V/AudioPolicyManager( 83): getDeviceForStrategy() from cache strategy 0, device 2
V/AudioPolicyManager( 83): getDeviceForStrategy() from cache strategy 0, device 2
V/AudioPolicyManager( 83): getNewDevice() selected device 2
V/AudioPolicyManager( 83): setOutputDevice() output 1 device 2 delayMs 0 force 0
V/AudioPolicyManager( 83): setOutputDevice() setting same device 2 or null device for output 1
D/Unity (13042): Creating OpenGL ES 1.x context (RGB16 565 24/8)
W/EglHelper(13042): createContext com.google.android.gles_jni.EGLContextImpl@40635d28 tid=11
E/SensorManager(13042): registerListener :: handle = 0 name= BMA023 Acceleration Sensor delay= 20000 Listener= com.unity3d.player.UnityPlayer@4052f380
E/SensorManager(13042): reg :: handle = 0
E/SensorManager(13042): registerListener :: handle = 1 name= MS-3C Magnetic Sensor delay= 20000 Listener= com.unity3d.player.UnityPlayer@4052f380
W/GLThread(13042): egl createSurface
W/EglHelper(13042): createSurface() tid=11
W/GLThread(13042): onSurfaceCreated
E/SensorManager(13042): reg :: handle = 1
W/GLThread(13042): onSurfaceChanged(800, 480)
D/Unity (13042): Android Unity internal profiler stats:
D/Unity (13042): cpu-player> min: 3.4 max: 11.8 avg: 5.4
D/Unity (13042): cpu-ogles-drv> min: 1.3 max: 146.3 avg: 7.2
D/Unity (13042): cpu-present> min: 0.9 max: 1564.0 avg: 61.5
D/Unity (13042): frametime> min: 9.7 max: 1573.2 avg: 74.2
D/Unity (13042): draw-call #> min: 22 max: 22 avg: 22 | batched: 109
D/Unity (13042): tris #> min: 4929 max: 4947 avg: 4932 | batched: 5672
D/Unity (13042): verts #> min: 5110 max: 5141 avg: 5115 | batched: 3384
D/Unity (13042): player-detail> physx: 0.3 animation: 0.3 culling 0.0 skinning: 0.4 batching: 0.5 render: 2.9 fixed-update-count: 0 .. 8
D/Unity (13042): mono-scripts> update: 0.5 fixedUpdate: 0.0 coroutines: 0.0
D/Unity (13042): mono-memory> used heap: 520192 allocated heap: 643072 max number of collections: 0 collection total duration: 0.0
D/Unity (13042): ----------------------------------------
It seems that the focus is lost for Unity Player automatically (I didn’t press home, pause or back buttons) resulting in loosing the OpenGL context. Some forum threads read this is usual behavior for Android, and I even tried the Application.runInBackground = true; without any success. I assumed the lag could be a result of Unity engine loading all the resources again and tried with few of meshes on the scene but found no improvement.
I have tested the same application on iPhone and didn’t find any lag similar to Android.
Is there anyway I can avoid releasing OpenGL context in the middle of gameplay? Or any other solution to solve this lag?
(I am using Unity 3.4 with Android 2.3.3 SDK on Samsung Galaxy S)