How to stop excessive Object creation on Samsung phones?

匿名 (未验证) 提交于 2019-12-03 03:05:02

问题:

I am have problems with animations slowing down on many Android Samsung phones. I have traced the problem and it is too much garbage collection. However, none of the objects are being created directly by my code.

This is the stack trace from the Allocation Tracker in DDMS.

  at com.samsung.android.multiwindow.MultiWindowStyle$1.createFromParcel(MultiWindowStyle.java:493)    at com.samsung.android.multiwindow.MultiWindowStyle$1.createFromParcel(MultiWindowStyle.java:491)    at com.samsung.android.multiwindow.IMultiWindowFacade$Stub$Proxy.getMultiWindowStyle(IMultiWindowFacade.java:499)    at com.samsung.android.multiwindow.MultiWindowFacade.getMultiWindowStyle(MultiWindowFacade.java:81)      at android.app.Activity.getMultiWindowStyle(Activity.java:4681)      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1688)       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1264)     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6614)       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)     at android.view.Choreographer.doCallbacks(Choreographer.java:613)    at android.view.Choreographer.doFrame(Choreographer.java:583)    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)      at android.os.Handler.handleCallback(Handler.java:733)       at android.os.Handler.dispatchMessage(Handler.java:95)       at android.os.Looper.loop(Looper.java:146)       at android.app.ActivityThread.main(ActivityThread.java:5678)  

This is creating thousands of Rect and Point objects when the user touches the screen.

Does anyone know of a work-around?

I am not targeting Samsung phones or using and of their sdks or libraries.

回答1:

Well, there is a design pattern comes into play which we use for situation like this, Called flyweight pattern. Not sure if this helps you if the client code is under your control , if it is , rather making the new objects, use the same object, with the properties of the object ( for example Rect will have x,y coordinates ) stored in the client object in an array , and call the draw method on the Rect object , passing the coordinates of the rect object stored in the array in the client.



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