# K&B - Chapter 3 - Self test 10 - GC

Leonardo Pereira
Greenhorn
Posts: 2
Hi. I could not understand the exercice 10, chapter 3 from K&B.
Please, take a look in the code below:

According to the book, when "//do stuff" is reached, five objects were created and two are elegible for GC.
In my opinion there are 3 objects that are elected to GC. Did I missing anything?
Leo

Henry Wong
author
Marshal
Posts: 21007
77

Okay, I will fix the first line for you...

Leonardo Pereira wrote:

No. Only one object (an array object) is created.

Henry

Shaikh Ali
Ranch Hand
Posts: 51
Leonardo Pereira wrote:Hi. I could not understand the exercice 10, chapter 3 from K&B.
Please, take a look in the code below:

According to the book, when "//do stuff" is reached, five objects were created and two are elegible for GC.
In my opinion there are 3 objects that are elected to GC. Did I missing anything?
Leo

Objects created:
Line 6 - the array object - 1
Line 7 - one Dozens object and one int[] object - 2
Line 8 - one Dozens object and one int[] object - 2
Total - 5

After Line 10 no active references point to objects created in Line 8, so 2 objects are eligible for GC.

Vijitha Kumara
Bartender
Posts: 3913
9
And welcome to CodeRanch, Leonardo Pereira

Leonardo Pereira
Greenhorn
Posts: 2
Thank you guys for your support.
I was really lost on this question.
This subject is much more clear now.
Cheers,
Leo

Scott Gordon Sutherland
Greenhorn
Posts: 9
• 1
Hi

I'd like to clarify the interpretation of when the objects are created - can someone explain where I go wrong and miss the 4th and 5th creation and 2nd eligible for GC

Dozens [] da = new Dozens[3];
//One object is created (ie a single Array 'da' with four index options (ie [0,1,2,3]), so while the Array is an Object the index positions are not. An Array holds 'references to' not objects per se. <Objects created =1>

da[0] = new Dozens();
//One object is created (ie a 'Dozens' and is referenced or indexed to position "da[0]" <Objects created =2>

Dozens d = new Dozens();
//One object (referenced with 'd') is created of the type 'Dozens'. This happens to be an Array given the class Dozens. <Objects created =3>

da[1] = d;
//The reference to object 'd' is now associated to index position in the Array "da[1]". Noting that while Arrays are objects the index links to a reference of the object, it does not hold the object as such. The object retains its place in the heap. (ie KB SCJP v6 pg257). <Objects created =3> because there is no new object - 'da[1]' is now referencing the same position (or object) on the heap as 'd' was. Object 'd' however is implicitly eligible for GC as it has been redirected to 'da[1]' (ie KB SCJP v6 pg258 example - line12). So <Objects for GC =0> as the object referenced by 'd' is not available for GC, the actual object has been re-directed to another reference, so there is nothing for GC, as the object is still accessible by a thread via 'da[1]'.

d = null;
//The reference 'd' is now explicitly nulled to make it eligible for GC, thereby elimating the ability of a thread to find the object it referenced on the heap. But the object itself is not available for GC as it has already been re-directed to the reference 'da[1]'.

da[1] = null;
//The reference to the object (originally obtained when 'da[1]=d') is nulled from the array reference 'da[1]'. The Array 'da' now no longer "knows" about the object received 'd'. Hence as no thread can access it then <Objects for GC =1>

Thorsten Schneider
Greenhorn
Posts: 16
• 1
You miss the creation of objects of type int[] whenever an instantiation of type Dozens occurs. These int[] can no longer be reached, if you can't reach the object referencing them.

So, let's start this over:

Vijitha Kumara
Bartender
Posts: 3913
9
Scott Gordon Sutherland wrote:
Dozens [] da = new Dozens[3];
//One object is created (ie a single Array 'da' with four index options (ie [0,1,2,3]),...

It is an array of size three (indexes of 0,1 and 2)

Ranch Hand
Posts: 31
Thorsten Schneider wrote:You miss the creation of objects of type int[] whenever an instantiation of type Dozens occurs. These int[] can no longer be reached, if you can't reach the object referencing them.

So, let's start this over:

Ranch Hand
Posts: 31
Thorsten Schneider wrote:You miss the creation of objects of type int[] whenever an instantiation of type Dozens occurs. These int[] can no longer be reached, if you can't reach the object referencing them.

So, let's start this over:

hey hi Thorsten
i have not understood line 8 saying

you said that object created 5 here how it becomes 5 here.
you specified some relation with int[] object how it applies to this line..please please please make me clear

cheers

nitin sethi
Ranch Hand
Posts: 59

I hope the below explanation clears your doubt. If a class contains an object as its instance member, creating that class's object creates one more object corresponding to that instance member.

Regards,
Nitin Sethi

Ranch Hand
Posts: 31
Thanks a lot nidhi now i understand...>>>

Christian Joseph
Ranch Hand
Posts: 43

I hope the below explanation clears your doubt. If a class contains an object as its instance member, creating that class's object creates one more object corresponding to that instance member.

Regards,
Nitin Sethi

if i put another int to that "IntObjClass" it will create 2 additional?

so after the main run, it will create "IntObjClass" object, and Integer object, and an "int" object? ?
which has a total of 3?

Ranch Hand
Posts: 64
int is not an object it is an primitive type variable

Christian Joseph
Ranch Hand
Posts: 43
so what happened here is obj is pointing two objects(new IntObjClass and Integer Object) on the HEAP. so once the obj is declared null, the two are left on the blackhole(i prefer this term instead of heap )

Christian Joseph
Ranch Hand
Posts: 43

I hope the below explanation clears your doubt. If a class contains an object as its instance member, creating that class's object creates one more object corresponding to that instance member.

Regards,
Nitin Sethi

hey another Question what if the Integer "obj" instance variables is not declared. will it create an object if i type "new IntObjClass"

dennis deems
Ranch Hand
Posts: 808
Christian Joseph wrote:
hey another Question what if the Integer "obj" instance variables is not declared. will it create an object if i type "new IntObjClass"

Yes, there just wouldn't be a reference to the object.

Ranch Hand
Posts: 64
Christian Joseph wrote:so what happened here is obj is pointing two objects(new IntObjClass and Integer Object) on the HEAP. so once the obj is declared null, the two are left on the blackhole(i prefer this term instead of heap )

This is Mr Naveen Madarapu bro.

Vijitha Kumara
Bartender
Posts: 3913
9
Christian Joseph wrote:...will it create an object if i type "new IntObjClass"

The type here is "IntObjClass" only and the new keyword is used to create an object of the given type.

Christian Joseph
Ranch Hand
Posts: 43
Thank you so much guys!!