SurfaceView 奇怪的行为 Android 内存不足错误
我正在做 http://www.droidnova.com/2d -tutorial-series-part-ii,772.html 已完成,但我正在创建 SurfaceView 的新实例,导致内存不足异常。
我在 Eclipse MAT 的帮助下完成了分析,它告诉我 SurfaceView 从未消亡。虽然我正在杀死正在运行 onDraw 的线程以及活动,但它仍然存在于内存中。 有什么想法吗?
I am doing what http://www.droidnova.com/2d-tutorial-series-part-ii,772.html has done but I am making new instances of SurfaceView that are causing Out of memory exception.
I have done profiling with the help of Eclipse MAT and it showed me that SurfaceView has never died. although I am killing the thread that is running onDraw and also the activity but still it remains present in memory.
any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能应该通过在其父项上调用
ViewGroup#removeView(surfaceView)
来从视图层次结构中删除不使用的 SurfaceView。SurfaceView
和一般对象在没有其他对象引用它们之前无法进行垃圾回收。您还可以考虑重用 SurfaceView,而不是创建新的 SurfaceView。
You should probably remove SurfaceViews that you aren't using from the view hierarchy by calling
ViewGroup#removeView(surfaceView)
on their parents. TheSurfaceView
, and objects in general, can't be garbage collected until no other objects are referencing them.You might also consider reusing SurfaceViews instead of creating new ones.
我对 SurfaceViews 也有同样的问题。我添加了一个
View
并将其删除,但垃圾收集器并没有销毁它。问题是我覆盖了SurfaceView中的方法
onDetachedFromWindow
,并且没有调用super.onDetachedFromWindow()
。这样,SurfaceView
注册了一个 CallBack 并且没有取消注册它。I had the same Problem with SurfaceViews. I added a
View
and removed it, but it the Garbage Collector did not destroy it.The problem was that I overwrote the method
onDetachedFromWindow
inSurfaceView
and did not callsuper.onDetachedFromWindow()
. This way theSurfaceView
registered a CallBack and did not unregister it.