每次我创建新的Thread时,它都被添加到主ThreadGroup中,甚至我将它仍然存在于主ThreadGroup中的线程中,从而导致内存泄漏。请帮忙
更新
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("mThread", "Surface Destroyed Called");
getHolder().removeCallback(this);
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
Log.d("mThread", "b4 Interrupted");
_thread.interrupt();
Log.d("mThread", "b4 thread group Interrupted");
_thread.getThreadGroup().interrupt();
Log.d("mThread", "b4 join");
_thread.join();
retry = false;
} catch (InterruptedException e) {
Log.d("mThread", "Interrupted");
Thread.currentThread().interrupt();
_thread.getThreadGroup().list();
_thread = null;//======>here nulling thread
break;
}
}
}
答案 0 :(得分:4)
问题不在于它被添加到线程组中。已终止的线程将始终(最终)从线程组中删除。
如果应用程序泄漏内存,则代码中存在错误。你正在咆哮错误的树。
答案 1 :(得分:-2)
如果线程存在于ThreadGroup中并且您需要将其删除,则可以使用ThreadGroup类的.remove()
方法。它从ThreadGroup组中删除指定的线程。
语法:
void remove(Thread t);
// t is thread to be removed from the ThreadGroup.