1

I am a little confused on whether or not this situation is dangerous:

I have an object which include a reference to a callback listener. At a certain event in the object the callback listener will be called. The implementation of the abstract callback function will then remove the only existing reference to my object. When the callback returns I do other stuff in the event handler and may also manipulate members.

Since the only reference to this object now is gone, will the Dalvik JVM garbage collector attempt to garbage collect my object even though I am doing the remaining stuff in the event handler? And what will the consequences be of this?

I have written some simple and rather useless code to illustrate my situation:

public SomeClass {

  public abstract class CallBackListener {
    public void abstract callback();
  }

  private CallBackListener mCb = null;
  private OrgObject mObject = null;

  public SomeClass() {
    mCb = new CallBackListener() {
      @Override
      public void callback() {
          mObject = null;
      }
    };
    mObject = new OrgObject(mCb);
  }
}

public class OrgObject {

  private SomeClass.CallBackListener mCb = null;

  public OrgObject(SomeClass.CallBackListener cb) {
    mCb = cp;
  }

  public void event() {
    //call callback
    cb.callback();

    //do other stuff
  }
}

Any inputs on this situation and/or the practice?

Thanks in advance

4 Answers4

2

No; objects accessible to a running thread are not eligible for garbage collection.

While you are executing a method that belongs to an object, the object itself is always accessible to the thread using the this reference (stored on the thread execution stack), so it cannot be garbage collected. The object might be gc'd when the thread exits the method, if no other references to it remain.

Joni
  • 108,737
  • 14
  • 143
  • 193
1

At any moment anything that could possibly be accessed by any thread, now or in the future, will not be garbage collected.

More concrete and very much simplified this is the process: the garbage collector starts with static fields and automatic variables, including implicit 'this' variables. From there it finds everything (indirectly) accessible from those, including implicit references from inner classes to outer ones. Everything not found is then finalized and deleted.

So while your event() method is executing, an implicit automatic 'this' variable is found and your OrgObject is not garbage collected.

Guus Bloemsma
  • 31
  • 1
  • 6
0

This sort of explains it

But the accepted answer does not take the callback issue into account. But a comment suggests that the THIS reference also counts as a valid reference....but when is this invalidated and open for garbage collection?

Community
  • 1
  • 1
  • The call frame on the stack includes all of the arguments to a method, including the "this" pointer. Thread stacks are part of the GC root set. So if a non-static method is being executed, the associated object will not be collected. References are not invalidated; either the GC can find them from the root set or it can't. – fadden Sep 26 '13 at 14:41
0

While event() method is executing you do have a reference to OrgObject (mObject). It's called this. So no, GC won't collect your object. Besides, it would be rather bizzare if it collected "executing" class. Don't you think?

joozek
  • 2,143
  • 2
  • 21
  • 32
  • Absolutely! But I also think that my code is rather bizarre, but it was the only way I could think of solving my problem :) – user2500605 Sep 26 '13 at 08:47