• 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.

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, This is my first post on the Forum. I am preparing for SCJP exam. I couldnt clear first attempt(55 %).

I was wondering if anybody could please help me to clear some of my concepts on GC.

This is from Question 4 of Diagnostic Test - Devaka Cooray.
Please could anyone explain how this works. Thanks in advance.
Q4.JPG
[Thumbnail for Q4.JPG]
Question 4 - Diagnostic Test- Devaka Cooray
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mushraf,
welcome to Javaranch!
When it comes to Garbage collection, an image is worth a thousand words, if you REALLY want to solve garbage collection problems ONCE-AND-FOR-ALL practice diagram drawing, it is NOT difficult, it took me ONLY 90seconds to solve this problem.

The answer is ONE object is 'eligible' for garbage collection. Which is the object referenced by 'a2.a'
I guess you REMEMBER that objects are 'eligible' for garbage collection when they have NO 'live reference' variables connecting them.

The ONLY way I can effectively explain this to you is via diagram drawing. (I promise to do that later today).

Regards

Ikpefua.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing, while you await my diagram explanation click on this LINK, when you are there scroll down to the bottom of the page, its an example of how I used diagram to resolve a garbage collection puzzle in this forum. Check it out, it might even give you a 'clue' on how I got the answer to your question.
 
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mushraf,

I also agree that one Object is eligible for garbage collection. The one created with newA(null) and to which a2.a was refferring to.

Regards
Deepa
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class_A_Heap.jpg
[Thumbnail for Class_A_Heap.jpg]
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The numbers placed in the reference variables fletchers correspond to each line of execution in the program I wrote... study it CAREFULLY. If you have doubts, I will be pleased to clarify them.

The diagram clearly demonstrates the answer.

REMEMBER that every instance of this class has its own 'COPY' of the rereference variable 'A a' declared in line 2 of the program.
During instantiation whatever argument you pass to the constructor is assinged to the reference variable
'A a'

I hope this helps.

Regards

Ikpefua.
reply
    Bookmark Topic Watch Topic
  • New Topic