• 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 assigning null value

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can any one help me in this qtn from javaprepare.com exam1
Q) At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.

void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4
a)Before statement labeled 1
b)Before statement labeled 2
c)Before statement labeled 3
d)Before statement labeled 4
e)Never.

Correct Answer is D) as given in that site.
i am not clear with this topic.
suppose an object has been assigned a null value in a method.
will it be available for GC immediately
(OR)
will it be available for GC only after that method is finished executing.
i will appreciate if anyone can explain me in detail.
Thanks bye
rohan

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rohan mehta:
Hi,
Can any one help me in this qtn from javaprepare.com exam1
Q) At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.

void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4
a)Before statement labeled 1
b)Before statement labeled 2
c)Before statement labeled 3
d)Before statement labeled 4
e)Never.

Correct Answer is D) as given in that site.
i am not clear with this topic.
suppose an object has been assigned a null value in a method.
will it be available for GC immediately
(OR)
will it be available for GC only after that method is finished executing.
i will appreciate if anyone can explain me in detail.
Thanks bye
rohan


Hello Rohan,
First, let's take a look at statement
String r = new String ("abc");
This statement creates a memory address called object reference that points to another memory block that contains "abc".
Second, the same thing happens in the next statement,
String s = new String ("abc");
Again this statement creates another memory address or an object reference that points to another memory block that contains "abc".
So the two statements create two different addresses / object references that point to two different memeory blocks that contain exact the same content, "abc"
Now, let's take a look at the statement at line //3, s = s + r. This statement will create a new address or object reference to store the result from the RHS expression. So after line //3, which is now line //4, the memory block that contain the string "abc" that was created for the old s which now has no reference that points to it. Hence that block of memeory is now READY for GC at line //4. Note the word READY does mean only that "hey GC dude, you can collect this block anytime you are ready." It however does not say "GC get rid of it right now."
By the way, line //4 happens to be at the end of the method in your example. It does not have to be. It could be another statement in the body of that method. So the answer should have been "old s is ready for garbage collection right after s points to new memory block (i.e next instruction in the code)."
Hopes that helps.
Take care,
Lam


[This message has been edited by Lam Thai (edited April 18, 2001).]
 
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
good explaination , Lam. What you said about line 4 thing also is correct. I hope Rohan , your doubt is clear.
Your Friendly bartender
Shailesh.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic