1

Every time I create new Thread it is being added to main ThreadGroup and even I null the thread it still exists in main ThreadGroup causing Memory Leak. please help

Updated

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;
            }
        }
    }
AZ_
  • 21,688
  • 25
  • 143
  • 191
  • I don't have much code to show. please help – AZ_ Jun 10 '11 at 10:17
  • `and even I null the thread it still exists in main ThreadGroup` .. What does that mean ? – Saurabh Gokhale Jun 10 '11 at 10:24
  • 1
    any thread that normally (or abnormally but still manages to call `exit()` from native code) will be evicted from the owning threadGroup. Basically you need the thread to cease its execution. – bestsss Jun 10 '11 at 11:01
  • I can not call stop it is deprecated – AZ_ Jun 10 '11 at 11:19
  • how to stop a thread execution ? please help – AZ_ Jun 10 '11 at 11:20
  • call stop, if nothing else helps. *deprecated* doesn't mean unavailable. if you know what you do, you can use `stop()`, stop is just `interrupt()` + exception (ThreadDead) on the next available instruction/safe point. btw calling interrupt on the threadgroup could be far worse, if you have not created the group yourself. – bestsss Jun 10 '11 at 11:30

2 Answers2

3

The problem is not that it is added to the thread group. A thread that has terminated will always (eventually) be removed from the thread group.

You have a bug in your code, if the application is leaking memory. You are barking up the wrong tree.

Kaj
  • 10,862
  • 2
  • 33
  • 27
  • please have a look here too http://stackoverflow.com/questions/6304879/every-time-creating-new-thread-is-being-added-to-main-threadgroup/6305122#6305122 – AZ_ Jun 10 '11 at 10:40
  • *referenced by anyone will always*, that has nothing to do w/ threadGroup. When a thread exits it is removed from the group. That's all. – bestsss Jun 10 '11 at 11:00
  • @bestsss. Thanks, removed that part. – Kaj Jun 10 '11 at 11:05
-2

If the thread is existing in your ThreadGroup and you need to remove it, you could use .remove() method of ThreadGroup class. It removes the specified thread from ThreadGroup group.

Syntax :

void remove(Thread t);  
// t is thread to be removed from the ThreadGroup.
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
  • how to use this method? Thread.currentThread.getThreadGroup() doesn't give me access. it has package level access. I have my own class that extends thread – AZ_ Jun 10 '11 at 10:27
  • @Aizaz : You need to invoke the `.remove(Thread t)` on the ThreadGroup instance you get from `Thread.currentThread.getThreadGroup()`. – Saurabh Gokhale Jun 10 '11 at 10:29
  • it just gives me resume() because it is (package private) void remove(Thread t) – AZ_ Jun 10 '11 at 10:34
  • please have a look here too http://stackoverflow.com/questions/6304879/every-time-creating-new-thread-is-being-added-to-main-threadgroup/6305122#6305122 – AZ_ Jun 10 '11 at 10:39
  • @roadrunner, this is a package private method and you should not call it on your own (through reflection/setAccessible), dunno who voted for the answer but it makes no sense. – bestsss Jun 10 '11 at 11:06
  • what do you mean by "through reflection/setAccessible" ? – AZ_ Jun 24 '11 at 09:13