Lots of allocations in Libgdx slow down game and cause sprite-jolts

旧巷老猫 提交于 2019-12-13 12:44:36

问题


I found out that LIBGDX allocates a lot of variables (not from any of my methods). I just ran the game for a few seconds and I've already got 32000 rows in DDMS->Allocation Tracker. The methods that allocate are getPalmRejection, createFromParcel and nativeReadString. Is it normal to have this many allocations in just a couple of seconds?

I get 60FPS but sometimes it drops to 58-59 and that is important because I use framerate-independent movement in my game and I get alot of sprite-jolts (sprite-jumps) when this FPS drop happens.

Here are the stack traces :

**createFromParcel**
  at android.graphics.Rect$1.createFromParcel(Rect.java:562)    
at android.graphics.Rect$1.createFromParcel(Rect.java:557)  
at com.samsung.android.multiwindow.MultiWindowStyle.readFromParcel(MultiWindowStyle.java:278)
at com.samsung.android.multiwindow.MultiWindowStyle.<init>(MultiWindowStyle.java:141)   
at com.samsung.android.multiwindow.MultiWindowStyle$1.createFromParcel(MultiWindowStyle.java:284)   
at com.samsung.android.multiwindow.MultiWindowStyle$1.createFromParcel(MultiWindowStyle.java:282)   
at com.samsung.android.multiwindow.IMultiWindowFacade$Stub$Proxy.getMultiWindowStyle(IMultiWindowFacade.java:467)   
at com.samsung.android.multiwindow.MultiWindowFacade.getMultiWindowStyle(MultiWindowFacade.java:53) 
at android.app.ActivityThread.getAppMultiWindowStyle(ActivityThread.java:5368)  
at android.app.ContextImpl.getAppMultiWindowStyle(ContextImpl.java:2698)    
at android.content.ContextWrapper.getAppMultiWindowStyle(ContextWrapper.java:690)   
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6324)  
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6307)   
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6278)  
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6243)  
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6483)  

**<init>**
  at com.samsung.android.multiwindow.MultiWindowStyle.<init>(MultiWindowStyle.java:40)  
at com.samsung.android.multiwindow.MultiWindowStyle$1.createFromParcel(MultiWindowStyle.java:284)   
at com.samsung.android.multiwindow.MultiWindowStyle$1.createFromParcel(MultiWindowStyle.java:282)   
at com.samsung.android.multiwindow.IMultiWindowFacade$Stub$Proxy.getMultiWindowStyle(IMultiWindowFacade.java:467)   
at com.samsung.android.multiwindow.MultiWindowFacade.getMultiWindowStyle(MultiWindowFacade.java:53) 
at android.app.ActivityThread.getAppMultiWindowStyle(ActivityThread.java:5368)  
at android.app.ContextImpl.getAppMultiWindowStyle(ContextImpl.java:2698)    
at android.content.ContextWrapper.getAppMultiWindowStyle(ContextWrapper.java:690)   
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6324)  
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6307)   
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6278)  
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6243)  
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6483)  
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)  
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)   
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)   


**getPalmRejection**
  at android.view.ViewRootImpl.getPalmRejection(ViewRootImpl.java:3895) 
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6389)  
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6307)   
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6278)  
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6243)   
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6483)  
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)  
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)   
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)   
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6456)  
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6502)    
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)    
at android.view.Choreographer.doCallbacks(Choreographer.java:603)   
at android.view.Choreographer.doFrame(Choreographer.java:571)   
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789) 
at android.os.Handler.handleCallback(Handler.java:733)  

EDIT: One very probable cause is the MultiWindow feature of Samsung devices . Another possible cause of the allocations is in the methods of the touchDown event of the game screen:

@Override
public boolean touchDown (int x, int y, int pointer, int button) {
    if ( (!spaces_can_explode && !paused_game_screen) && !spaces.exploded_finished && hud_display.startTimerFinished){
        hud.touchDown(x, y, pointer, button, spaces, spaceship_bounding_rectangle, display_fud_camera, hud_display.startTimerFinished);
    }
    return false;
}

public void touchDown(int x, int y, int pointer, int button, Ship spaces, Rectangle shootHud, OrthographicCamera hud_cam, boolean startTimerFinished){
    hud_cam.unproject(touchPoint.set(x, y,0));
    pressed_middle_shoot = shootHud.contains(touchPoint.x, touchPoint.y);           
    if(!(pointer>1) && spaces.touch_upped){
        pressed_left = (x < screen_sizewp3) && !pressed_middle_shoot;
        pressed_right = (x > screen_sizewo2p3) && !pressed_middle_shoot;            
        pressed_middle = (!pressed_left && ! pressed_right);
        if(!spaces.reversed_rotation ){
            if(pressed_middle){
                pressed_shoot = pressed_middle_shoot;
                spaces.pressed_shoot = pressed_middle_shoot;
                pressed_accel = !pressed_shoot;
                spaces.pressed_accel = !pressed_shoot;
            }
            else if(pressed_left && !pressed_right){
                if(spaces.sprite.getX() > spaces_maxleftplus){
                    spaces.touchDOWN(x, y, pointer, button, pressed_left, pressed_right, pressed_middle);
                }
            } else if (pressed_right && !pressed_left){
                if(spaces.sprite.getX() < spaces_maxrightminus){
                    spaces.touchDOWN(x, y, pointer, button, pressed_left, pressed_right, pressed_middle);
                }           
            }
        }
    }       
    pressed_left_hud = pressed_left;   
    pressed_right_hud = pressed_right;
    pressed_shoot_once = pressed_middle_shoot;
}

来源:https://stackoverflow.com/questions/29287679/lots-of-allocations-in-libgdx-slow-down-game-and-cause-sprite-jolts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!