开发者

Android: GC_FOR_MALLOC caused by a lengthy touch event?

I've been reading around and looking into touchEvents, mainly because my GC is exploding when there is a lengthy touch/slide event OR many touch events. If I do not touch the phone it simply idles as ~开发者_如何转开发5 objects as you can see from the first few GC_EXPLICIT that I performed from DDMS. I then began to touch the screen and sliding around, and the objects shot up around 13513 objects and actually caused a GC_FOR_MALLOC that takes over 100ms. Here was my simple testing code, and below is the log of the dalvicvm tag. If you have documentation of workarounds or causes, or possibly even just another in-depth discussion of this I would greatly appreciate it! Cheers, and good luck on your own endeavors.

[code]

    public class testClass extends Activity implements IOnSceneTouchListener{   
        int numberOfTouches = 0;        

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.main);
        }

        @Override   
        public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {                       
            numberOfTouches++;              
            return false;   
        } 

    }

[logcat]

06-28 15:24:55.317: DEBUG/dalvikvm(2501): GC_EXPLICIT freed 5 objects / 128 bytes in 53ms
06-28 15:24:55.903: DEBUG/dalvikvm(2501): GC_EXPLICIT freed 5 objects / 136 bytes in 59ms
06-28 15:24:56.708: DEBUG/dalvikvm(2501): GC_EXPLICIT freed 5 objects / 128 bytes in 59ms
06-28 15:25:06.614: DEBUG/dalvikvm(2501): GC_EXPLICIT freed 6 objects / 168 bytes in 58ms
06-28 15:25:09.833: DEBUG/dalvikvm(2501): GC_EXPLICIT freed 7 objects / 192 bytes in 65ms
06-28 15:25:14.270: DEBUG/dalvikvm(2501): GC_EXPLICIT freed 8 objects / 232 bytes in 59ms
06-28 15:25:18.294: DEBUG/dalvikvm(2501): GC_EXPLICIT freed 6 objects / 160 bytes in 59ms
06-28 15:25:33.942: DEBUG/dalvikvm(1103): GC_FOR_MALLOC freed 13513 objects / 1403264 bytes in 121ms
06-28 15:26:53.684: DEBUG/dalvikvm(2139): GC_EXPLICIT freed 140 objects / 6960 bytes in 99ms
06-28 15:26:58.731: DEBUG/dalvikvm(1215): GC_EXPLICIT freed 668 objects / 71136 bytes in 117ms
06-28 15:27:31.637: DEBUG/dalvikvm(1103): GC_FOR_MALLOC freed 13962 objects / 1504296 bytes in 122ms
06-28 15:27:44.723: DEBUG/dalvikvm(2501): GC_EXPLICIT freed 63 objects / 2152 bytes in 59ms
06-28 15:27:46.676: DEBUG/dalvikvm(2501): GC_EXPLICIT freed 5 objects / 128 bytes in 65ms
06-28 15:27:47.238: DEBUG/dalvikvm(2501): GC_EXPLICIT freed 5 objects / 128 bytes in 58ms

I've yet to actually solve the problem but have stumbled across this great article on this exact problem: Android Touch Problems

[edit] as stracka said, it's most likely due to flooding. My real issue is however with the allocations that are being made on each event? Is there a way to reuse these events/objects to limit allocations due to touch?

I'm currently using andEngine, and the touchEvents are pooled so there should be at most ~5 ever allocated from scratch; otherwise they just reuse don't they?

Thanks for any ideas....


Are you using an older version of Android? I was just reading about the "touch event flood" problem on Android 1.5 in Beginning Android Games. The author's workaround was to put the UI thread to sleep briefly in his onTouch() handler, to limit the number of events received. I don't know how viable that solution is, or if would be of any use to you; the pertinent page from the book can be found here:

Beginning Android Games, page 131


        @Override
        public boolean onTouchEvent(MotionEvent event) {
            numberOfTouches++;
            //events can be handled as well.
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    invalidate();
                    break;
            }
            return true;
        }

This goes light on GC and mallocs. This is the override method of a View. Try it and reply..

android.view.View.onTouchEvent(MotionEvent event)

Reference:

http://developer.android.com/reference/android/view/View.html#onTouchEvent%28android.view.MotionEvent%29

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜