简体   繁体   中英

Will Dalvik JVM garbage collect a non-referenced object in which I am executing code and handling members?

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

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.

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.

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM