• Post Reply Bookmark Topic Watch Topic
  • New Topic

When GC comes?

 
umar hathab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have declared one variable inside a method like
String strName =null;
I am doing some operations on that variable and exiting from the method..I have read that we should make the variable as null inside finally black or at the end of method..But my question is since strName is local variable it will be GC'ed automatically when the method returns..Is it necessary to make all the local variables as null at the end of method...what will happen if I declare one class level (global variable) and have not assigned null to that variable..?when it will be GC'ed?
thanks,
A.Umar
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, notice that local variables don't get gc'ed (as they reside on the stack, not on the heap), but objects do.
You are right, though, that a local variable gets destroyed as soon as the method is exited (sometimes even more early). If an object is referenced only by a local variable, it can get gc'ed when this variable gets out of scope.
So, no, you don't need to set any local variable to null before exiting a method.
Does that help?
 
umar hathab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ilja..
yeh..I am quite clear now...can you tell me when the global variables are GC'ed when not explicitly set null..?
Thanks..
A.Umar
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!