GLThread参考导致Android游戏泄漏内存 [英] Android Game Leaking memory because of GLThread reference

查看:204
本文介绍了GLThread参考导致Android游戏泄漏内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在用Android(OpenGL ES 1.1)开发游戏,并且看到每创建一个新SurfaceView(GLView)都会创建其线程.没关系.当我要完成保存SurfaceView的Activity(并返回菜单)时,就会出现问题.似乎没有释放活动,因为每个GLThread都在引用它.这可能会因OOM错误而结束.

Right now I'm developing a game in Android (OpenGL ES 1.1) and I'm seeing that whenever I create a new SurfaceView (GLView) its thread is created. That's ok. The problem comes when I want to finish the Activity that holds the SurfaceView (and go back to the menu). It seems that the activities are not released because each GLThread is referencing it. This may finish with an OOM error.

一些MAT图片:

第一张图片:MarkitActivity代表与SurfaceView关联的Activity的每个实例.

The first picture: the MarkitActivity represents each single instance of the Activity that deals with SurfaceView.

第二张图片:内存中所有活动的列表.

The second picture: The list of all the activities in memory.

第三张图:什么是来自GC的活动.

The Third Picture: What is holding the Activities from GC.

如果需要任何代码,我将其发布.尽管如此,我已经尝试了以下方法:

If any code is needed I will post it. Nevertheless I have already tried the following things:

->活动上下文对渲染器和曲面视图的引用不足.

->Weak reference of the Activity Context to the renderer and to the surfaceview.

->应用程序上下文而不是活动上下文(在正常和弱模式下).

->Application Context instead of Activity Context (in normal and weak mode).

->尝试停止(以困难的方式)线程(中断)并等待加入(程序执行该操作,但该线程不在乎,它仍然存在...)

->Trying to stop (in a hard way) the thread (interrupt) and waiting for join (Which the program does it, but the thread does not care, it is still there...)

->尝试不进行调试,以防万一在调试器模式下值发生变化(MAT图片没有调试器).

->Trying without debugging, just in case in debugger mode the values changes (the MAT pictures are without debugger).

->尝试将Activity作为singleInstance模式.到处都是奇怪的结果和错误.

->Trying the Activity as singleInstance mode. Weird results and errors everywhere.

->已正确控制了视图的onPause和onResume.

->onPause and onResume are correctly controlled for the view.

任何提示,想法,问题或帮助将不胜感激.预先感谢!

Any hint, idea, question or help will be really appreciated. Thanks in advance!

卡洛斯.

推荐答案

我在线程上也遇到了类似的问题(但没有使用OpenGL),最后使用一个简单的技巧解决了这个问题.

I had a similar problem with threads (but not using OpenGL), and end up solving it using a simple trick.

在进行活动(在onPause()onStop()中)之前,请尝试使该线程为空:

Before exting activity (in onPause() or onStop(), try nulling the thread like this:

myThread = null;

似乎可以收集Thread GC,因此您的活动也可以收集.

It seems that it makes the Thread GC collectable, and therefore you activity becomes also collectable.

它对我以及对我提出相同建议的一些类似问题的人都有效.

It worked for me, as well as for some people with similar problems to whom I gave the same suggestion.

致谢.

这篇关于GLThread参考导致Android游戏泄漏内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆