• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variables created in Static method  RSS feed

 
Archies Gomes
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have a doubt regarding memory allocations in java,
What I understand from memory allocations to a program is ,
All the instance variables and objects created using the new operator are stored on the heap
And all the static variables created are stored in a special area called as static area.
So Once a class loads, all the static variables are initialized and the static methods been loaded.
Now what i wanted to make clear is ,
what happens when I create some objects in a static method,Can these objects be garbage collected once the execution of this method is over ??because what I could see is objects created in static method ,were still seen on heap...a Example of this was ,
whenever we call function trim() in string ,it internally calls the substring method ,which returns

new String(offset + beginIndex, endIndex - beginIndex, value)

so now for example if i'm doing something like,

So whenever now this MyClass is loaded ,myMethod would get executed as its a static one,so after execution of this method ,would the Strings temp and a be eligible for garbage collection or not?and do we refer to them as static variables because they are created in a static method?

Thanks All,Appreciate your valuable time.
[ September 18, 2006: Message edited by: Archies Gomes ]
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All objects are created on the heap.

Variables created in a method (whether it be static or not) are local variables. The are eligible for garbage collection at the end of the method call and are recreated at the start of the method call.

However if you created a object in the method and passed the reference out, then the object wouldn't be eligible for garbage collection if the caller kept the reference.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim LeMaster:
Variables created in a method (whether it be static or not) are local variables. The are eligible for garbage collection at the end of the method call and are recreated at the start of the method call.


Local variables are never garbage collected. They live on the method stack and therefore simply disappear immediately when the method exits.

Objects referenced by a local variable are therefore immediately eligible for gc if there are no other references left.
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep you are absolute correct, thanks for that clarification.
 
Archies Gomes
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables are never garbage collected. They live on the method stack and therefore simply disappear immediately when the method exits.

So what if a string is created inside a static method,it would be still created on heap itself right?
may be the methods get executed on the stack and all the other references and primitive type remains on stack ,but what i fell is the objects would still get the memory from heap itself right?so would all these memory would get cleared(ofcourse not referenced by outside entities in the mthod) on the Garbage collection call?
Thanks,Appreciate your valuable time.
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep no matter where the reference to the object is declared all objects are created on the heap.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!