SurfaceView 奇怪的行为 Android 内存不足错误

发布于 2024-10-22 03:54:00 字数 303 浏览 3 评论 0原文

我正在做 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

緦唸λ蓇 2024-10-29 03:54:00

您可能应该通过在其父项上调用 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. The SurfaceView, 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.

三寸金莲 2024-10-29 03:54:00

我对 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 in SurfaceView and did not call super.onDetachedFromWindow(). This way the SurfaceView registered a CallBack and did not unregister it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文