• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When GC comes?

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic