• 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 collector example

 
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At what point will the object created on line 8 be eligible for garbage collection?

public class RJMould{
StringBuffer sb;
public static void main(String argv[]){
RJMould rjm = new RJMould();
rjm.kansas();
}
public void kansas(){
sb = new StringBuffer("Manchester");
StringBuffer sb2 = sb;
StringBuffer sb3 = new StringBuffer("Chester");
sb=sb3;
sb3=null;
sb2=null;
}
}


1) Line 11
2) Line 9
3) Line 12
4) Line 13

I was trying above example from link
http://www.jchq.net/certkey/0301certkey.htm

did not understand THE ANSWER.It said line 13. I was thinking line 11 as that particular line sb is assigned to sb3.

I was trying to understand the answer.


Any links, ideas, resources,sample code highly appreciated. thanks in advance.








Which of the following statements are true?

1) finalize will always run before an object is garbage collected
2) finalize may run before or after an object is garbage collected
3) finalize will run when an object becomes unreachable
4) finalize allows a programmer to free memory allocated to an object

Finalize will always be run before an object is garbage collected. It cannot run after it is collected because by then the object will cease to exit. When an object becomes unreachable it will be eligible for garbage collection but there is no guarantee when finalize will run, only that it will run before garbage collection happens. The final option is a passable description of destructors in C++ but not of the finalize method in Java.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to read the code completely, and go through it with a pencil and paper, marking which object is pointed to by which reference variable, and rubbing out references as they change.
 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was trying on a paper with pencil.

sb, sb2 refer to Mancheser stringbuffer object where as sb3 point to Chester stringbuffer objects. when sb2 points to Mancheser stringbuffer object then sb disconnected from Mancheser stringbuffer object thus when you say sb2=null sb is eligible for garbage collection. please advise
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mathew Lee wrote: . . . when you say sb2=null sb is eligible for garbage collection. . . .

Correct

That is, I believe line 13, which was the answer given on the website.
 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>>when sb2 points to Mancheser stringbuffer object then sb disconnected from Mancheser stringbuffer object

why does sb disconnected from Mancheser stringbuffer object. i was not clear on that. please advise
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mathew Lee wrote:

Which of the following statements are true?

1) finalize will always run before an object is garbage collected
2) finalize may run before or after an object is garbage collected
3) finalize will run when an object becomes unreachable
4) finalize allows a programmer to free memory allocated to an object

Finalize will always be run before an object is garbage collected. It cannot run after it is collected because by then the object will cease to exit. When an object becomes unreachable it will be eligible for garbage collection but there is no guarantee when finalize will run, only that it will run before garbage collection happens. The final option is a passable description of destructors in C++ but not of the finalize method in Java.



Not exactly.

A developer might call finalize() on an object. If she does (which she shouldn't because it's a bad habit), finalize() will still run if the object gets garbage collected. She might call finalize() a hundred times and the object will still be fine, alive and well to continue to do object things.

Also,
There is no (none, zero, zip) guarantee that finalize() will ever be called on an object. The only guarantee is that finalize() will be called zero or at most one time by the JVM. This is because the GC may never run, or may run and not GC that object.

And furthermore,
It's worth saying here that if, for some reason, the JVM's call to finalize() on an object happens to resurrect it somehow -- finalize() will never be called on that object again by the JVM... even if it's eligible for GC.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mathew Lee wrote:>>>when sb2 points to Mancheser stringbuffer object then sb disconnected from Mancheser stringbuffer object

why does sb disconnected from Mancheser stringbuffer object. i was not clear on that. please advise



So basically, 2 objects are created Manchester StringBuffer and Chester Stringbuffer.
At the end of line 10, sb and sb2 reference Manchester, while sb3 reference Chester.
After the execution of (line 11) sb=sb3, => sb2 reference Manchester, while sb and sb3 reference Chester. Note Manchester doesn't qualify for GC because sb2 is still referencing the object
Only after the execution of (line 13) sb2 = null, => Manchester is not reference, hence it is qualify for GC.

I am looking at the problem from the perspective of when the object is not reference, I feel that it will be easier.


 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Janeice, are you suggesting those questions are inaccurate?
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Janeice, are you suggesting those questions are inaccurate?



I was suggesting there is a technical nuance / semantic here:

Finalize will always be run before an object is garbage collected



Saying the word "always" with the phrase "before an object is GC" is dangerous.

The finalize method MAY NOT be run at the time when an object is destroyed. It may have been called by the JVM already and will never be called by the JVM again. So, technically, before an object is GC is correct (because things have to happen to an object before it's destroyed, as OP noted), but that's (in my opinion) not necessarily the spirit of the question because some folks get hung up thinking that it HAS to happen when the object is GC -- right before. The danger is in relying on the finalize() method to close resources or do anything important, because there is no guarantee it will run at the time of Object death, or at all.

And adding clarification here:

there is no guarantee when finalize will run, only that it will run before garbage collection happens.



... because the finalize() method may never be called at all. The object may never GET destroyed, even if it's eligible. Another technical semantic.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Janeice
 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>>Only after the execution of (line 13) sb2 = null, => Manchester is not reference, hence it is qualify for GC.

sb2=null,

does it null sb2 object reference on stack or Manchester String Buffer object on heap.


when sb2=null does that means sb=null automatically?

please advise



 
Sze Kong Chan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mathew Lee wrote:
sb2=null,
does it null sb2 object reference on stack or Manchester String Buffer object on heap.


it set the value of the sb2 reference to null, Manchester StringBuffer is still on the heap.

Mathew Lee wrote:
when sb2=null does that means sb=null automatically?


no.




 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then as per the question how objet created in line 8 ie

>>sb = new StringBuffer("Manchester");

is eligible for garbage collection. please advise
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I though you had already worked that out: there are no longer any references pointing to it.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic