gudii9
asked on
garbage collector exaple
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.
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.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
>>>sb3 (and sb2) will *reference* the same object referenced by sb
you mean sb and sb2?
I was thinking those two refer to Mancheser stringbuffer object where as sb3 point to Chester stringbuffer object.
>>it creates one StrongReference object sb2 and it points to the StringBuffer("something") in the heap.
does sb drops or disconnected pointing to StringBuffer("something") in the heap. at this point.
What is meaning of StrongReference object. will there be weak StrongReference object also.
>>StrongReference object sb2 is garbage collected.
you mean eligible for garbage collection or garbage collected?
May be if you can send small picture or graphical representation of how to understand this concept that would be great. please advise
you mean sb and sb2?
I was thinking those two refer to Mancheser stringbuffer object where as sb3 point to Chester stringbuffer object.
>>it creates one StrongReference object sb2 and it points to the StringBuffer("something") in the heap.
does sb drops or disconnected pointing to StringBuffer("something") in the heap. at this point.
What is meaning of StrongReference object. will there be weak StrongReference object also.
>>StrongReference object sb2 is garbage collected.
you mean eligible for garbage collection or garbage collected?
May be if you can send small picture or graphical representation of how to understand this concept that would be great. please advise
sorry yes
sb = new StringBuffer("Manchester") ;
sb -> Manchester
StringBuffer sb2 = sb;
sb -> Manchester
sb2 -> Manchester
StringBuffer sb3 = new StringBuffer("Chester");
sb -> Manchester
sb2 -> Manchester
sb3 -> Chester
sb=sb3;
sb -> Chester
sb2 -> Manchester
sb3 -> Chester
sb = new StringBuffer("Manchester")
sb -> Manchester
StringBuffer sb2 = sb;
sb -> Manchester
sb2 -> Manchester
StringBuffer sb3 = new StringBuffer("Chester");
sb -> Manchester
sb2 -> Manchester
sb3 -> Chester
sb=sb3;
sb -> Chester
sb2 -> Manchester
sb3 -> Chester
ASKER
>>>
sb=sb3;
sb -> Chester
sb2 -> Manchester
sb3 -> Chester
i understood till here. But how making sb2=null makes sb null at line 13 please advise
sb=sb3;
sb -> Chester
sb2 -> Manchester
sb3 -> Chester
i understood till here. But how making sb2=null makes sb null at line 13 please advise
it doesn't
ASKER
so how the answer is line 13 which basically asks
At what point will the object created on line 8 be eligible for garbage collection?
please advise
At what point will the object created on line 8 be eligible for garbage collection?
please advise
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
sb3 (and sb2) will *reference* the same object referenced by sb
It will not be available for gc until there are no references to the object