Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection - Practice

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am having a problem attempting questions on garbage collection. I think the way I approach to attempt the question is I am trying to draw some diagrams like...whenever I see an new reference variable, I am pointing to the object that it should be linked to. After that when the variable is reassigned to something else, thats where I fail to redraw. I know that I have to remove the arrow pointing to previous reference and now point to the newly assigned one.
Can somebody please help me understand how I should attempt GC questions...I am getting most of the questions wrong because of my approach. May be I still did not get the concept right.
Here is a simple example I have from whizlabs diagnostic exam.

How many object references will be refering to "Garbage" object at the end of the code.
So this is my approach
x---------> new Integer object 10
y---------> new Long object 100
z ---------> new String object "Garbage"
obj ------> now pointing to the object refered by x (i.e) 10. At this point Integer obj 10 has 2 reference variables x,obj
x--------->z. That means x is now pointing to Garbage. At this point x's reference to 10 is removed and arrow is drawn to "Garbage".
z --------->y. That means z is now pointing to Long obj 100. At this point z's reference to "Garbage" is removed.
y---------->x. That means y is now pointing to Garbage. At this point string obj "Garbage" can be reached by two references x,y.

Please tell if my approach is correct. Should I follow the same approach for all GC questions.
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
excellent..why would you want to change this
 
You showed up just in time for the waffles! And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic