Tuesday, October 23, 2007

Good example of Garbage Collection.

If u are very much confused with how garbage collection works in Java, then the following example might really be helpful to u in understanding the concept clearly.

class Animal {}
class Cat extends Animal {}
class Dog extends Animal {}

public class GarbageCollection
{
public static void main(String[] args)
{
Animal a1 = new Cat();
Animal a2 = new Dog();
Animal a3 = a1;
a1 = null;
Animal a4 = a1;
Animal a5 = a2;
a1 = a2;
a2 = a4;
a5 = null;
a3 = a1;

// Here
}
}


When that code reaches the comment "Here," how many objects will be eligible for garbage collection? Well, like I said before, it's all about drawing pictures. Let me take you through my process for solving such a problem. I'll go line my line and, after each line, I'll draw a picture (I'll do my best using ASCII art) to reflect what has happened thus far. Just keep in mind that an object is eligible for garbage collection as soon as there are no active references to it.


Animal a1 = new Cat();

+-----+
a1 ---> | Cat |
+-----+


As you can see, we now have a variable named a1 that references a Cat object. That's easy enough, let's move on to the next line.


Animal a2 = new Dog();

+-----+
a1 ---> | Cat |
+-----+

+-----+
a2 ---> | Dog |
+-----+


Now, we still have a1 referencing that Cat object but we now have a new variable, a2, which references a Dog object. Let's move on.


Animal a3 = a1;

+-----+
a1 ---> | Cat | <--- a3 +-----+ +-----+ a2 ---> | Dog |
+-----+


The new variable a3 now also references the Cat object.


a1 = null;

a1 ---> null

+-----+
a3 ---> | Cat |
+-----+

+-----+
a2 ---> | Dog |
+-----+


a1 now references null, but, as you can see, nothing is yet eligible for garbage collection as both the Cat and Dog objects have an active reference to them.


Animal a4 = a1;

a1 ---> null
a4 ---> null

+-----+
a3 ---> | Cat |
+-----+

+-----+
a2 ---> | Dog |
+-----+


That line didn't change our picture much. We now have a new variable, a4, that also references null.


Animal a5 = a2;

a1 ---> null
a4 ---> null

+-----+
a3 ---> | Cat |
+-----+

+-----+
a2 ---> | Dog | <--- a5 +-----+


We have a new variable, a5, introduced to the mix and it now also references the Dog object.


a1 = a2;

a4 ---> null

+-----+
a3 ---> | Cat |
+-----+

+-----+ <--- a5 a2 ---> | Dog |
+-----+ <--- a1


Now we have three references to the Dog object, but that one reference to the Cat object, a3, keeps it from being eligible for garbage collection.


a2 = a4;

a4 ---> null
a2 ---> null

+-----+
a3 ---> | Cat |
+-----+

+-----+
a1 ---> | Dog | <--- a5 +-----+


We've lost one of our references to the Dog object, but we've still got two left.


a5 = null;

a4 ---> null
a2 ---> null
a5 ---> null

+-----+
a3 ---> | Cat |
+-----+

+-----+
a1 ---> | Dog |
+-----+


Well, we've lost yet another reference to the Dog object. We're down to one, but one reference is still enough to keep that object from being garbage collected.


a3 = a1;

a4 ---> null
a2 ---> null
a5 ---> null

+-----+
| Cat |
+-----+

+-----+
a1 ---> | Dog | <--- a3 +-----+


Our final line of code and we finally have an object available for garbage collection. By assigning the reference to the Dog object to a3, we no longer have any variables that reference our Cat object. Therefore, the Cat object is now available for garbage collection.

That's it, really. I know it seems simple, but if you keep drawing pictures, I think you'll find that you have little trouble with questions regarding garbage collection. Hopefully, this little tip helps you out along the way.

Thanks to Corey! for this wonderful example.

No comments: