I have a GameScene which has a character (animated sprite), AutoParallaxBackground background with quite large textures and no more than 3 other items attached to the scene at a time, previous 3 items are dispose()-ed accordingly, background music and item sounds. Background music is instantiated with MusicFactory, Sounds are played on SoundPool. In between switching these scene we are loading resources asynchronously, reading the streams and instantiation is done in AsyncTask and the loading textures to the hardware is done on UpdateThread/GLThread. If I turn off the background music, this starts to happen totally random.

I'm experiencing scene junking on at first flow: MenuScene -> 1st Stage (GameScene). After a while lag does not happen, rather than occasionally or random. Junk is happening when aforementioned 3 items are attached on the Scene and a sound is played along, note that background music is playing at the same time.

Detaching previous 3 items is "optimized" to reduce GC calling almost perfectly. When the lag occurs GC_EXPLICIT is showed in the logcat.

What I have tried:

  • Playing the sounds and music on different thread, IntentService, dedicated Service.
  • Reduced calls to mScene.detachChild(child) and other scene methods to avoid iterations over children of the scene.

  • Detaching IEntitys with matcher to avoid iteration.

  • Inspected sound files' integrity with ffmpeg for possible errors

Could .dispose(), unloading the textures from hardware or detachChild() causing this junk?

Could be marked object for GC from the async tasks be causing this?

What else could be contributing to this issue?


After we have optimized the attaching and got rid of unnecessary look ups for scene children we've gained more fps as we advanced.

Occasional hiccups that were caused by GC_EXPLICIT were happening because Font in update() was explicitly calling System.gc(). Removing this line we were back on track below the green line, yeah! 60fps.

However, the author of AndEngine might had reasons for this explicit call. Since the stages are short we granted our app call this gc() when the GameScene is paused, quit or stage complete, anywhere where there is no excessive animation.


Another thing that could contribute to the fps drop is the SoundPool. Every time SoundPool is done playing with sounds in the pool, it resets its state to idle perhaps, when the next sound is played it takes a 8ms+ to reload its state to different state than idle. To avoid this you need to play looping sound with 0 left and right volume.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.