0

Let's say I have such a class:

class C
{
   int a, b;
   ArrayList<int> c = new ArrayList();
};

and such a collection:

ArrayList<ArrayList<C>> MyList = new ArrayList<ArrayList<C>>();

Would calling MyList.clear() nullify all the references to every ArrayList in the MyList and any additional objects inside of the each ArrayList in MyList? So, more precisely, will all elements inside class C.c be nullified as well as C.a and C.b?

Savail
  • 867
  • 10
  • 27
  • See this thread - http://stackoverflow.com/questions/3798424/what-is-the-garbage-collector-in-java – Tesseract Jul 13 '14 at 11:37
  • Java, simply put, automates the pointer process for you. It creates a variable in memory, points to it, breaks that pointer when you tell it to, and then the garbage collector notices there isn't a reference to it and eats it. So when you clear an array you are just breaking all of your pointers to space in memory. They are still there until they get garbage collected, but there is nothing pointing to them. At least this is my understanding. – zgc7009 Jul 13 '14 at 11:38

2 Answers2

6

No, but if nothing else has reference to them, they will be eligible for garbage collection.

E.g., simpler example:

class Foo {
    Date dt;

    Foo() {
        this.dt = new Date();
    }
}

ArrayList<Foo> list = new ArrayList<Foo>();
list.add(new Foo());
list.clear();

As of the clear, the Foo object's dt has not been "nulled" or anything, but the Foo object and the things only it refers to (the Date inside it, in this case) are eligible for garbage collection.

Whereas:

class Foo {
    Date dt;

    Foo() {
        dt = new Date();
    }
}

ArrayList<Foo> list = new ArrayList<Foo>();
Foo f = new Foo();
list.add(f);
list.clear();

Here, the Foo object isn't eligible for garbage collection, because f still refers to it even though the list doesn't. Of course, if f is a local variable in a method, then as soon as the method returns, f goes out of scope — at which point, if nothing else has a reference to the object, it's eligible for GC. But that's beside the point, the point is that when the clear completes, the object is still referenced (by f).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • hmm, regarding your second example, if f was a local variable, then after leaving the local space it wouldn't refer to the object anymore? And then it would be eligible to garbage collection? – Savail Jul 13 '14 at 11:51
  • @Savail: Yes, once `f` goes out of scope, if nothing else has a reference to that `Foo` object, the object becomes eligible for GC. – T.J. Crowder Jul 13 '14 at 13:10
3

If c is the only object that contains strong references to the contained lists then eventually they'll be garbage collected.

They won't be directly emptied though, just removed from the containing list. For example

ArrayList<C> inner = new ArrayList<C>();
inner.add(new C());

ArrayList<ArrayList<C>> outer = new ArrayList<ArrayList<C>();
ArrayList<ArrayList<C>> outer2 = new ArrayList<ArrayList<C>();

outer.add(inner);
outer2.add(inner);

outer.clear();

In this situation inner will be removed from outer but not from inner2, and since there will be another strong reference to inner, it's not eligible for garbage collection. Otherwise it will be released evenually, but all at once, not by emptying the inner elements.

Jack
  • 131,802
  • 30
  • 241
  • 343