Android Game Leaking memory because of GLThread reference

倖福魔咒の 提交于 2019-12-12 17:22:19

问题


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.

Some MAT pictures:

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.

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...)

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

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

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

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

Carlos.


回答1:


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

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

myThread = null;

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.

Regards.



来源:https://stackoverflow.com/questions/13367800/android-game-leaking-memory-because-of-glthread-reference

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