This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

chapter 3 self test question 10, K&B

 
Mauricio Archbold Babroza
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why are Five objects were created and Two objects are eligible for GC???
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mauricio. If you search the forum first, you'll find a lot of threads discussing the same problem.
 
Mauricio Archbold Babroza
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
corret me if i wrong,
Dozens [] da = new Dozens[3]; --> 1 object
da[0] = new Dozens(); 2 objects , one is the object dozens and the other is an array of int dz
Dozens d = new Dozens(); 2 objects , one is the object dozens and the other is an array of int dz
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes your interpretation is correct.

Earlier you might have missed the array. Array of Dozens is also an Object.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yes there are two objects for garbage collection.



If you see this snippet taken from the code, here first a object of dozens was created and was referenced
by a reference d. Then da[1] also started pointing towards the same dozens object after statement 2. Now
remember that, when you create the the dozens object another array object is also created which is called
da.

If you observe statement 3 and 4 then you can find out now both the references which were pointing towards
the Dozen object has been set to null, so there is no way to access that object nor any alive reference to da,
so now the dozen object and array object both are eligible for garbage collection.

Best Regards,
 
hasi weragala
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
made a rough drawing in paint , so that it will be easy to imagine which objects are going to be available for GC!

 
Jan Cumps
Bartender
Posts: 2584
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi "De Coder",
Please check your private messages for a JavaRanch administrative matter.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

De Coder, please change your display name to point towards your original name. It is against
the policy of java ranch to put fake names for display.

Best Regards,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic