When Is The Object Eligible For Garbage Collection?

后端 未结 7 649
后悔当初
后悔当初 2020-11-27 07:22

In the code below, given that amethod has been called. At what point/line is the Object originally referenced by myObject, eligible for Garbage Col

7条回答
  •  [愿得一人]
    2020-11-27 08:13

    The object will not become a candidate for garbage collection until all references to it are discarded. Java objects are assigned by reference so when you had

       classObject = myObject;
    

    You assigned another reference to the same object on the heap. So this line

       myObject = null;
    

    Only gets rid of one reference. To make myObject a candidate for garbage collection, you have to have

      classObject = null;
    

提交回复
热议问题