This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When // doStuff is reached, how many objects are created within main() {specify them} and which one's are eligible for GC?

i think 4 objects would be created i.e.
-c1 and Short variable contained in it
-c2 and Short variable contained in it
----only c1 and it's instance variable Short would be eligible for GC
but i have one doubt would c3 be eligible for GC too ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also do you have any guesses of your own about how many objects are eligible for GC??
 
dipayan chatterjee
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohit

i agree with Ankit , first try out the problem yourself and post what you think the answer would be, we can then identify where you went wrong( if your answer is wrong) in this way you will learn more buddy. so post your answer and we are always there to help you out
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
answer posted
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohit your answer is right. 4 objects were created and 2 were eligible for GC. As far as c3 goes, it never pointed to any object, it was null. c2 will not be eligible as passing it to go method doesn't makes it null (read this for more info)...
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IF I REPLACE THE LINE
Short story = 200;
WITH
Short story;

then ,would there be 4 object created or 2 objects as no value would be assigned to story instance variable ?
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't assign story any value, then it won't point to any object so only 2 objects will be created (as you said yourself)...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic