Reputation: 3
Im having a little problem in my Libgdx Android game. When I change the screen, animation starts as soon as new screen created. It works perfectly fine and smooth on the Desktop, but when i run this on android device, the lag appears when new screen created and it looks like a few frames of animation are just skipped.
After some investigation i discovered that this is because creating new screen invokes many new images and other objects to be created, therefore GC_FOR_ALLOC was invoked several times:
04-23 20:26:11.411: D/dalvikvm(18461): GC_FOR_ALLOC freed 385K, 48% free 8809K/16880K, paused 12ms, total 12ms
04-23 20:26:11.531: D/dalvikvm(18461): GC_FOR_ALLOC freed 454K, 48% free 8867K/16880K, paused 12ms, total 12ms
04-23 20:26:11.651: D/dalvikvm(18461): GC_FOR_ALLOC freed 475K, 48% free 8903K/16880K, paused 13ms, total 13ms
04-23 20:26:11.746: D/dalvikvm(18461): GC_FOR_ALLOC freed 473K, 48% free 8942K/16880K, paused 12ms, total 13ms
04-23 20:26:11.856: D/dalvikvm(18461): GC_FOR_ALLOC freed 481K, 47% free 8967K/16880K, paused 12ms, total 12ms
So my question is: Can I somehow make my app wait for these operations to complete and only after that launch the animation?
In fact, I need the ability to get the garbage collectors 'state' or something, so that I could start animation after gc has finished its allocation operations.
Thanks in advance for any help!
Upvotes: 0
Views: 106
Reputation:
I don't know Libgdx but i see it has a Gdx.graphics.getFramesPerSecond() method so maybe you can start your animation only when FPS stabilizes. If that method takes too long to give a suitable average perhaps you can measure your own FPS from start with getRawDeltaTime().
Upvotes: 1
Reputation: 7749
Use System.gc()
. Documentation:
When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
See Waiting for GC to finish before starting game with Java?
Upvotes: 0