• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
Vasim Patel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vasim Patel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic