• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Garbage Collection

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this question in a JQplus question
Question ID :952739439060
Is this true or false?
option:
a. An object will be garbage collected if the last reference to it is removed.
Explanation of JQPlus:
Yes, although there is a possibility , there is no guarantee. It may not be collected till the end of the program.
Can anybody give an example on this point or further explanation on this point?
Thanks in advance
Padmini
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Padmini
I think the the explanation you provided is correct. From what i know, the garbage collection is platform dependent because the runtime system keeps track of the memory allocated and determine which is reuseable to be put back into the heap.
Even if the Memory space refered by the reference gets reset to 'null' so no reference is pointing it now, there might be a live thread during the execution of the program which may still have access to that memory location. So because of these possible live threads that might prevent the garbage collection, we can't really know when, or even whether the object will be garbaged collected at all. System.gc() only can suggest that garbage collection happen. There is another method called "finalize()" which is usually called before gc() but I'm not so sure. For the exam though i think just knowing when it object is eligible for gc is enough.
Anyone is welcomed to correct me and post follow ups. I fanyone can also explain difference between System.gc() and finalize() reply would be appreciated
thanks
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If put simply :
System.gc() is used to call garbage collector.
finalize() method is used to perform any sort of winding up operations before the object is actually garbage collected.
For eg : closing a network connection, or closing a file etc.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a very trickily worded question. I think the java specification calls for objects with no references to be garbage collected at some point but like you say this could be at the end of the program.
For the exam you need to know a) that you cannot force garbage collection, and b) at what point an object becomes eligible for garbage collection.
Here's a test for you - at what point does the object initially referenced by 'obj' become eligible for garbage collection in the following code:
class Test
{
public static void main( String args[] )
{
methodA();
}
public static void methodA()
{
String obj = new String( "hello" );
String str = obj;
obj = null;
}
}
A. Immediately following execution of the line "obj = null;" (i.e. as the method terminates).
B. Never in methodA().
???
 
kevin goon
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think answer is "never in the method" since the object is still being refered by str. if u set str= some other object, then it would be eligible for gc
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My answer too is the same as Kevin Goon's .
But my question still remains unanswered. If anybody can give me an example of this:
" An object will be garbage collected if the last reference to it is removed. " It would be very helpful.
Thanks in advance.
Padmini
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Padmini,
The example question given by Tony is a good answer to your question:
" An object will be garbage collected if the last reference to it is removed. "
String obj = new String( "hello" );
A reference to the memory holding the String 'hello' is created.
String str = obj;
A copy of the reference is put into str.
This means there are two references now. An object is only eligible when it is no longer being referenced.
obj = null;
The first reference is removed, but there is still str with a reference.
Only if you added the line 'str = null;' would the objected be able to be thrown in the garbage.
Hope this makes it a bit clearer.
Jon.
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jon Coltman:
[B]Padmini,
The example question given by Tony is a good answer to your question:
" An object will be garbage collected if the last reference to it is removed. "
----------
Hi Tony and Jon,
Let me have an example like this:
String obj = new String( "hello" ); //Line 1
String str = obj; //Line2
String xys = str; //Line3
Now , the last reference to the string "hello" is xyz.
So I set xyz to null.
Now,
" An object will be garbage collected if the last reference to it is removed. "
As per this logic, will the object be Garbage collected???
Thanks
Padmini
.
[This message has been edited by padmini Babu (edited June 21, 2001).]

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi padmini,
An object will become candidate for garbage collection untill unless it's reference is lost by the programme(or you may say "solution space").
In all the above programmes which you and Tony has mentioned,
object reference is not loss.
consider the following code in which object "Hello" becomes a candidate for garbage collection.
class gc{
public static void main(String args[]){
String str1 = new String("Hello");
String str2 = "Bye";
str1 = str2; // After the execution of this statement
} // str1 will lost the reference of Hello
} // and Hello will be available for GC.
Hope this help..
Regards,
Hassan Naqvi.
 
padmini Babu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hassan Naqvi:
Hi padmini,
An object will become candidate for garbage collection untill unless it's reference is lost by the programme(or you may say "solution space").
In all the above programmes which you and Tony has mentioned,
object reference is not loss.
consider the following code in which object "Hello" becomes a candidate for garbage collection.
class gc{
public static void main(String args[]){
String str1 = new String("Hello");
String str2 = "Bye";
str1 = str2; // After the execution of this statement
} // str1 will lost the reference of Hello
} // and Hello will be available for GC.
Hope this help..
Regards,
Hassan Naqvi.


________________________________________________-_____
Hi Hassan,
You are right. In all the above programmes which I and Tony have mentioned,object reference is not lost.
*****
In my example.
String obj = new String( "hello" ); //Line 1
String str = obj; //Line2
String xys = str; //Line3
Now , the last reference to the string "hello" is xyz.
So I set xyz to null.
Now,
" An object will be garbage collected if the last reference to it is removed. " -- This statement is wrong here.
*******
In your example
class gc{
public static void main(String args[]){
String str1 = new String("Hello");
String str2 = "Bye";
str1 = str2; // After the execution of this statement
} // str1 will lost the reference of Hello
} // and Hello will be available for GC.
In your case, str1 is the first and last reference to the object containing "hello" . Hence it will be GCed.
So, the conclusion is for my question seems to me that " A object WILL NOT BE ELIGIBLE TO BE GCED always if the last reference to it is removed" -- THE REASON MAY BE BECAUSE THERE CAN BE OTHER REFERENCES POINTING TO IT EARLIER
AM I RIGHT? CINDY , SCOTT , JANE PL GIVE ME YOUR COMMENTS?

Padmini
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic