问题
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.
来源:https://stackoverflow.com/questions/27217001/how-to-stop-excessive-object-creation-on-samsung-phones