aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B - Chapter 3 - Self test 10 - GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B - Chapter 3 - Self test 10 - GC" Watch "K&B - Chapter 3 - Self test 10 - GC" New topic
Author

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

Leonardo Pereira
Greenhorn

Joined: Jan 19, 2011
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?
Thanks in advance,
Leo

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18136
    
  39


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

Leonardo Pereira wrote:



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

Henry
Faraz Kadri
Ranch Hand

Joined: Jan 26, 2011
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?
Thanks in advance,
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

Joined: Mar 24, 2008
Posts: 3775

And welcome to CodeRanch, Leonardo Pereira

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Leonardo Pereira
Greenhorn

Joined: Jan 19, 2011
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

Joined: Apr 18, 2011
Posts: 9
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

Joined: Apr 04, 2011
Posts: 16

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:



OCPJP 6
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

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)
aditya chaudhari
Greenhorn

Joined: Jul 05, 2011
Posts: 10

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:

aditya chaudhari
Greenhorn

Joined: Jul 05, 2011
Posts: 10

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
Aditya
nitin sethi
Ranch Hand

Joined: Jul 14, 2011
Posts: 59

Hi Aditya,

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
aditya chaudhari
Greenhorn

Joined: Jul 05, 2011
Posts: 10

Thanks a lot nidhi now i understand...>>>
Christian Joseph
Ranch Hand

Joined: Jan 07, 2011
Posts: 43
nitin sethi wrote:Hi Aditya,

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?
Naveen Madarapu
Ranch Hand

Joined: May 24, 2011
Posts: 64

int is not an object it is an primitive type variable
in your code


OCPJP
Christian Joseph
Ranch Hand

Joined: Jan 07, 2011
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 )

TY MS NAVEEN MADARAPU!
Christian Joseph
Ranch Hand

Joined: Jan 07, 2011
Posts: 43
nitin sethi wrote:Hi Aditya,

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

Joined: Mar 12, 2011
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.
Naveen Madarapu
Ranch Hand

Joined: May 24, 2011
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 )

TY MS NAVEEN MADARAPU!


This is Mr Naveen Madarapu bro.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

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

Joined: Jan 07, 2011
Posts: 43
Thank you so much guys!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B - Chapter 3 - Self test 10 - GC
 
Similar Threads
Page 274 Question 10 about Garbage Collection
chapter 3 self test question 10, K&B
HI please Help with. GC Kathy and Bert Book
Garbage Collection - P274 Kathy Sierra
Bert Bates Kathy Sierra Java 6 Study Guide