问题
I am creating many surface view instances to view some objects but one instance is created at a time and then surfaceDestroyed is called. but the thread I am creating every time on surfacecreated is being added to main ThreadGroup.
Although I am interrupting and nullifying it but it still resides in main ThreadGroup and creating low memory exception.
Code Snippets: constructor
public class MsurfaceView extends SurfaceView implements
SurfaceHolder.Callback {
_thread = new mThread(this);
_thread.setName("mThread");
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (!_thread.isAlive()) {
_thread = new BreedingThread(this);
}
_thread.setRunning(true);
_thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("mThread", "Surface Destroyed Called");
getHolder().removeCallback(this);
getHolder().addCallback(null);
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.interrupt();
_thread.getThreadGroup().interrupt();
_thread.join();
retry = false;
} catch (InterruptedException e) {
Log.d("mThread", "Interrupted");
// pass interrupt exception
Thread.currentThread().interrupt();
Log.d("mThread", "b4 threadGroupInterrupted");
_thread.getThreadGroup().interrupt();
_thread.getThreadGroup().list();//this shows thread is in //list
_thread = null;
break;
}
}
}
UPDATE Thread.list function shows that my interrupted and null thread are still in threadgroup
06-10 15:22:52.780: INFO/System.out(1814): java.lang.ThreadGroup[name=main,maxPriority=10]
06-10 15:22:52.780: INFO/System.out(1814): Thread[main,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-2,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Binder Thread #1,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Binder Thread #2,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[FlurryAgent,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[AsyncTask #1,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[AsyncTask #2,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-17,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[mThread,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[mThread,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[mThread,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-38,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Timer-2,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[mThread,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[mThread,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-53,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-286,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-327,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-359,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[mThread,5,main]
06-10 15:22:52.780: INFO/System.out(1814): Thread[Thread-409,5,main]
how to remove them ?
回答1:
Some things to look for.
- You are only calling _thread = null inside the catch block - this means it won't be set to null if the exception is never thrown. Move that inside a finally block.
- That exception is probably never thrown. Calling Thread.interrupt() will not throw it. That sets the thread's interrupt flag to true. That whole while loop is rather odd and you should probably re-write it.
- You are passing an instance of this to BreedingThread. Make sure that object is not holding a reference to your surface view forever
- Simply calling surface view destroy method does not mean the reference will be removed if something is still holding a reference to it (like I mention in 3 for example).
回答2:
When you call "new Thread" and do not specify a group, the thread will get added to the same group as the caller (i.e. Thread.currentThread().getThreadGroup()
). In your case, that's what is causing your threads to be added to the "main" thread group. To change that behavior, use new Thread(group, this)
to specify the group.
来源:https://stackoverflow.com/questions/6304879/every-time-creating-new-thread-is-being-added-to-main-threadgroup