简体   繁体   中英

GC_FOR_ALLOC causing lag in Android game

I am working on a cannon game for Android, which requires procedural generation of levels. As the player flies through the air, new sections get added on to the end of the level to keep it going indefinitely. As those sections are added, sections that the player has already passed are deleted. The problem is that the creation of a section usually leads to a GC_FOR_ALLOC, which pauses the game for around 30ms and causes noticeable lag.

The free memory stays fairly consistent, as objects are deleted while others are added, so there doesn't seem to be a memory leak. The engine I am using is the cocos2D-android-1 port from iOS.

I saw many solutions along the lines of "don't initialize things while the game is running", but the nature of the level generation requires this, since an indefinite-length level can't be created right at the start of the game.

Thanks for your help!

You are not very specific. If the lag is too big, you must try to reduce it.

One way could be to collect more often - resulting in shorter, but more often occurring pauses.

For example you might create smaller or only partial sections and try to nudge the garbage collector afterwards to run. You would use System.gc() for this. But be aware that System.gc() is not guaranteed to result in it actually starting to run.

An even better solution would be to reuse objects. Objects that were created before the level was started. This way even an endless level would not result in any garbage collection since no garbage accumulates. But be sure to not create any non-pooled objects!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM