• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

garbage collection

 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from this mock exam


Question 8. How many objects are eligible for garbage collection immediately after line 1? Select one correct answer.




A. 0
B. 1
C. 2
D. 3
E. 4



The answer given is D

Can someone explain how 3 objects are eligible for gc ? According to me, I do not find any single object for GC the answer should be A.

I think all the following objects are referring to valid memory locations.

q08
objArray
objArray[0]
objArray[1]

help me
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any object whose scope is limited to a method is eligible for garbage collection when the method returns, unless it is exported out of the method as a return value or exception. So in this case, the array object and its two elements are eligible after line 1.

Hope this helps.

Thanks,
Seema
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response. I modified the original code, after reading your explanation.



I ran the above code. According to your explanation, I should be getting 3 lines of "count" displayed for each of the objects created in the method and one more line of "count" because we are explicitly setting the q08 to null.
So total there should be 4 times "Count". However, the output I get is only
1 "Count" string. Any ideas ?

TIA
 
Seema Manivannan
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jay,

You have defined the finalize() method for the class Question08. So this method will be called only before garbage collection of objects of this class and not for any other object. So naturally, here it will be invoked only once.

Hope its clear now.

Thanks,
Seema
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Seema I get your point now...
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry but I too have to raise my hand for one question here. What about int i, should'nt we say that it needs to be garbage collected too ?.

And I am sorry for the earlier modification....My code only emphasizes your point.
[ December 04, 2004: Message edited by: seemapanth Joshi ]
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What about int i, should'nt we say that it needs to be garbage collected too ?.



"int i" is a primitive local variable, not an object. It is allocated on the stack, not the heap, and it goes away when doSomething() finishes. primitive local variables are never garbage collected because they are not on the heap, but they don't cause memory leaks because they never outlive their methods.
[ December 04, 2004: Message edited by: Mike Gershman ]
 
seemapanth Joshi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Gershman:


"int i" is a primitive local variable, not an object.



It does not qualify as an answer to the question originally asked that is "how many OBJECTS are available for garbage collection".

But I am not so sure about this


It is allocated on the stack, not the heap, and it goes away when doSomething() finishes. primitive local variables are never garbage collected because they are not on the heap.



The problem being this is purely theoritical and I want to know if question can come where it is asked when a variable is available for garbage collection. I quote from Marcus Green's tutotrial available on jchq.net


Once a variable is no longer referenced by anything it is available for garbage collection.


Link : http://www.jchq.net/tutorial/03_01Tut.htm

I am concerned if a question can be asked showing a variable and asked if it will be garbage collected and what would be the answer in that case. I came across such a question and am not able to locate it right now, shall reproduce it here as soon as I do. I may be WRONG. I want to be clear for the exam.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only objects (excepting string literals) are garbage collected. A method's local primitive variables and local object reference variables are not garbage collected. Member variables are parts of objects and are not considered as separate garbage collectable entities. A question should not ever say "when is variable x eligible for garbage collection?". It should be saying "when is the object referenced by variable x eligible for garbage collection?".
 
seemapanth Joshi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, much obliged !

I went back and saw a lot of stuff on this forum itself on this subject. And also I found this interesting link in my quest to understand GC on the web, in case anyone is interested : http://www.artima.com/insidejvm/applets/HeapOfFish.html

[ December 05, 2004: Message edited by: seemapanth Joshi ]
[ December 05, 2004: Message edited by: seemapanth Joshi ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic