wood burning stoves 2.0*
The moose likes Java in General and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

Vasim Patel
Ranch Hand

Joined: Apr 29, 2004
Posts: 87
Hi

take the case:

class Test
{
public static void main(String a[])
{
while (true)
{
new Test().foo();//1
}
}

Vector foo()
{
Vector v = new Vector();
v.add(new Object());//2
return v;//3
}
}

In this case, the local method foo() is creating a new vector and adding new object it to it and returning the vector. Pls note that I am not storing the return value in main().

In main(),
1.will the returned vector be eligible for garbage collection?
2. what will happen to a the returned objects which are not stored? will they be GCed?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The answer to both your questions is: it depends on the Garbage Collector. You'd have to look at the docs for whichever one you are using to know for sure.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Vasim Patel
Ranch Hand

Joined: Apr 29, 2004
Posts: 87
Thanks Paul.

My question was whether the object was eligible for being garbage collected not actually garbage collected. This I think is irrespective of which implementation of GC is being used.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

No. Its always entirely down to whatever algorithm the GC uses which decides what is or isn't eligable for collection.
Ashish Vegaraju
Ranch Hand

Joined: Aug 19, 2004
Posts: 47
hi Vasim,

yes the object is eligible for Garbage collection.
the Reason:

1. if any object is no longer referenced by the program, then such object become eligible for GC.

2.Paul, whatever alogithm the GC uses, the objective behind all those algorithms is to clean the heap that is occupied by unreferenced object...it doesnt matter if the algorithm is "tracing reference" algo or any other one.

hope it helps
Ashish.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection