aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC help needed" Watch "GC help needed" New topic
Author

GC help needed

ramya ray
Ranch Hand

Joined: Aug 11, 2006
Posts: 101
Hi, I have a query regarding garbage collection...

In gc , if an array is set to null, eligible for gc . does the elements of array which are referencing to some objects are also eligible,


Like :
class Test{

public object method(){

Object o=new Object();

Object [] oa=new Object[1];

oa[0]=o;

oa=null;

return oa;

}

}

when will obejct o eligible for gc ..after the return statement or after assigning oa = null;

Please help..
Also,
if i modify it like:

class Test{ public object method(){
Object o=new Object();
Object [] oa=new Object[1];

oa[0]=o;

o=null;

return o;
}
}

when the object referced o will be eligible for gc..

thanks,
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

In gc , if an array is set to null, eligible for gc . does the elements of array which are referencing to some objects are also eligible,


An array object is eligible for gc when it is no longer reachable. Setting an array reference that refers to the array, to null, doesn't mean that the array is eligible for gc. For example...



The array object that is referred to by "o" isn't eligible for gc, when the "o" variable is set to null. This is because it is still reachable by the "cpy" variable. The array object will be eligible when the cpy variable gets set to null, or goes out of scope.

This also applies to the elements in the array. For example...



In the case, the array object is not longer reachable, and hence eligible for gc. As for the elements: The index zero element is no longer reachable, and hence, eligible for gc. The index one element is still reachable via the "c" variable, and hence, not eligible for gc.

Which brings us to the last point...

when the object referced o will be eligible for gc..


You have to remember that objects are eligible for gc -- not references. They merely get accessed, changed (if not final), or go out of scope.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ramya ray
Ranch Hand

Joined: Aug 11, 2006
Posts: 101
Thanks a lot for making my doubt clear...
i have one more doubt , when a local object is eligible for gc ,after return statement or after end of method '}' if it is not goint to return.

like:
class Test{
public object method(){
Object o=new Object();
Object [] oa=new Object[1];
oa[0]=o;
o=null;
return o; // line 1
} //line 2
}

here in above code , object refernced o , is not eligible till end of method as oa[0] is refering to it. so it will be eligible for gc after line 1 or after line 2.

Thanks a lot ..
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

when a local object is eligible for gc ,after return statement or after end of method '}' if it is not goint to return.


An object is eligible for GC when it is no longer reachable, period. If a variable goes out of scope, it doesn't mean that the object is no longer reachable -- there may be another reference, or the object reference may be the returned value.

object refernced o , is not eligible till end of method as oa[0] is refering to it. so it will be eligible for gc after line 1 or after line 2.


In your example, does it make a difference? But if we modify the example a bit...



The answer is when the "oa" reference variable goes out of scope. This could be after line 1 or line 2 is executed, depending on exactly which return statement is actually executed.

Henry
ramya ray
Ranch Hand

Joined: Aug 11, 2006
Posts: 101
Thanks a lot Henry :-) I got it ...
 
wood burning stoves
 
subject: GC help needed