0

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!

2 Answers2

1

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().

  • Omg, you just solved my issue by mentioning getRawDeltaTime()! Previously I was calling render method with getDeltaTime() parametr, which returns smoothed value for _n_ last frames! As creating a new scene takes long time, average frame duration has increased for a while. And that was what I called a lag! Calling render method with getRawDeltaTime() solved that, as this method returns exact difference between two frames without smoothing! It appeared that I understood my problem wrong, garbage collector finished its job in the same frame when new screen was created :) Thanks for your help! – Semyon Komarov Apr 23 '16 at 20:08
0

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?

Community
  • 1
  • 1
F43nd1r
  • 7,690
  • 3
  • 24
  • 62
  • Thanks, but i have already tried that. This method has no use for me, as GC_FOR_ALLOC operations are invoked _during_ creation of a new screen and it freezes a few frames in the beginning. It happens even if `System.gc()` called before initialization of a new screen. I need a way to tell my game to hold on until garbage collector is done. – Semyon Komarov Apr 23 '16 at 19:33