Objects eligible for the AG

Asked

Viewed 83 times

3

I have the following certification issue on the Garbage Collector:

1: public class Rabbit {
2: public static void main(String[] args) {
3: Rabbit one = new Rabbit();
4: Rabbit two = new Rabbit();
5: Rabbit three = one;
6: one = null;
7: Rabbit four = one;
8: three = null;
9: two = null;
10: two = new Rabbit();
11: System.gc();
12: } }

And the following answers:

  1. Rabbit object in line 3 is first eligible for garbage collection immediately after line 8.
  2. Rabbit object in line 4 is first eligible for garbage collection immediately after line 9.

In answer two I understood that by putting 9: two = null; from this line he is already eligible for Garbage Collector.

However, at first you could explain to me why he becomes eligible after line 8. Once we have the line 7: Rabbit four = one; Following figure of what I understood doing as indicated by the official certification guide.

imagem dos objetos

2 answers

4


I think it’s easier to understand if we rework the drawings, seeing what happens line by line.


Line 3:

Rabbit one = new Rabbit();

An instance of Rabbit, for which the variable one points. That is to say:

linha 3

variable value
one Rabbit created on line 3

Line 4:

Rabbit two = new Rabbit();

We create another instance of Rabbit and assign in the variable two:

linha 4

variable value
one Rabbit created on line 3
two Rabbit created on line 4

Line 5:

Rabbit three = one;

The variable three points to the same place where one is pointing (that is to the Rabbit created in row 3):

linha 5

variable value
one Rabbit created on line 3
two Rabbit created on line 4
three Rabbit created on line 3

So far so good, we have 2 instances of Rabbit and three variables, of which two (one and three) point to an instance of Rabbit and a (two) points to the other instance.


Line 6:

Now I think that’s the point that’s got you confused:

one = null;

Here we make the variable one point to null. That is, it stops pointing to the instance of Rabbit:

linha 6

variable value
one null
two Rabbit created on line 4
three Rabbit created on line 3

Note that only the variable one is null, but three still pointing to the instance of Rabbit. And as each of the instances of Rabbit still contains references to them, none is eligible for the GC.

But why three is not null if it pointed to the same location as one? This happens because the variables are not the object itself, but references to it. Making an analogy, imagine that I have two sheets of paper and write my address on both. Both have a reference to the same address, but none of them is the actual location (they only point to the location, only have a reference indicating where it is). If I tear/burn/grind one of the sheets, the other will remain intact and pointing to the address.

That’s what happens here: one and three pointed to the same object (to the instance of Rabbit created in line 3). When leaving one null (by "tearing the paper"), only it ceases to point to the object, but three is not affected and continues pointing.


Line 7:

Rabbit four = one;

Here we create another variable (four) and make it point to the same place where one is pointing. But remember that one now is null, then four will also be:

linha 7

variable value
one null
two Rabbit created on line 4
three Rabbit created on line 3
four null

That is, the two instances of Rabbit continue to have references pointing to them, and therefore are not yet eligible for GC.


Line 8:

three = null;

Right now three becomes null:

linha 8

variable value
one null
two Rabbit created on line 4
three null
four null

And see that now yes the instance of Rabbit created in line 3 has no one else pointing to it, and therefore it becomes eligible for the GC.


Line 9:

two = null;

two becomes null, that is to say:

linha 9

variable value
one null
two null
three null
four null

So now the second Rabbit (that was created in the book 4) no longer has any reference to it, and therefore also becomes eligible for the GC.


Line 10:

two = new Rabbit();

A new instance of Rabbit, for which two points out:

linha 10

variable value
one null
two Rabbit created on line 10
three null
four null

It is worth remembering that call for System.gc() nay ensures that the AG will be executed. The only thing we know is that certain objects are eligible to be collected, but when this is done it’s something we don’t control.

  • What you used to make the drawings?

  • 1

    @NatanFernandes https://balsamiq.com/wireframes/

  • thanks to everyone who helped @hkotsubo in particular for their attention, God bless you

3

In 7: Rabbit four = one is assigning the variable value one for the variable four. And what is the value of the variable one at that time? Just look at the previous line that just assigned a value to one and the line is 6: one = null;, therefore in four the value to be assigned is a null.

What null has to do with any new Rabbit() created in the code? Nothing. For some reason you must think you have. Or you are ignoring line 6 that nullifies the value of one. four does not point to any object, does not point to the object created and stored in one right at the start.

This first object came to have two references to it, but never got to have three. So the moment the two references cease to exist the object is free to be collected.

For me these drawings are confusing, and at the very least it gives to understand something that does not happen, maybe it is wrong even, at least it is my interpretation, that I only affirm because of the confusion. He doesn’t show the note clearly, and it seems that four is pointing to Rabbit what never happens at any time has line 7 that makes four point to null.

  • then at this point: 5: Rabbit three = one; 6: one = null; after line 6 he would already be eligible since everyone who references one would be pointing to null would be that?

  • No, not everyone is pointing to null on line 6, only one is, the code says just that, nothing else.

  • but the fact of 5: Rabbit three = one; consequently three will not is pointing to null?

  • Read the line, it doesn’t say it’s pointing to null. Don’t see things that aren’t happening. That’s all the code says.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.