Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Leonardo Pereira wrote:



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

Henry
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 4101
64
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to CodeRanch, Leonardo Pereira
 
Leonardo Pereira
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for your support.
I was really lost on this question.
This subject is much more clear now.
Cheers,
Leo
 
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Greenhorn
Posts: 16
Eclipse IDE Firefox Browser Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 4101
64
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 31
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 31
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot nidhi now i understand...>>>
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int is not an object it is an primitive type variable
in your code
 
Christian Joseph
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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"
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4101
64
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much guys!!
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!