Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variables

 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where do the static variables are stored in java...?
 
Stephan van Hulst
Bartender
Posts: 5334
48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not exactly sure, but I believe they are stored in an instance of the Class class.
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi stephan,
thanks for the reply!

You mean to say on heap. Will you more elaborate your answer ?
Stephan van Hulst wrote:I'm not exactly sure, but I believe they are stored in an instance of the Class class.
 
amit mandal
Ranch Hand
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are stored in the Class object associated with that class. Instances of Class are also stored on the heap called "permanent generation".
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amit mandal wrote:They are stored in the Class object associated with that class.


Can someone confirm this? Or some kind of material where we can read about this. Even I never thought about this But I feel they would also be on the heap- May be all the statics in one group on the heap? That's actually coming back to what Amit said

amit mandal wrote:Instances of Class are also stored on the heap called "permanent generation".


I believe that the Object allocation and GC techniques are much complicated. There's a new generation and an old generation. Objects are allocated to the new gen. if these objects survive the GC then they are moved to the old gen.
 
amit mandal
Ranch Hand
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can read it here even though its bit confusing :

permanent generation
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amit mandal wrote:you can read it here even though its bit confusing :

permanent generation


Thanks for sharing the article
 
amit mandal
Ranch Hand
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohamed sanaullah wrote:
amit mandal wrote:you can read it here even though its bit confusing :

permanent generation


Thanks for sharing the article


You are welcome
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have recently gone through an article on the same and according to that:
" Whenever a process is loaded in the RAM, we can say that the memory is roughly divided into three areas (within that process):
1> Stack
2> Heap
3> Static.
The Static which is also a part of heap is used for storing static members and is also called "High Frequency Heap."

But I have a doubt, if static members exist on heap then when does the garbage collector sweep them off the heap or it wouldn't consider them?
 
amit mandal
Ranch Hand
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swaraj gupta wrote:I have recently gone through an article on the same and according to that:
" Whenever a process is loaded in the RAM, we can say that the memory is roughly divided into three areas (within that process):
1> Stack
2> Heap
3> Static.
The Static which is also a part of heap is used for storing static members and is also called "High Frequency Heap."

But I have a doubt, if static members exist on heap then when does the garbage collector sweep them off the heap or it wouldn't consider them?


I suppose the static variables reside inside the permanent generation part of the heap (which i suppose is same as the high frequency heap) and stays as long as their class objects remain there as dont forget for every class loaded the jvm creates an instance of Class. so as long as the Class object stays there, the static variable is safe from the GC.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swaraj gupta wrote:But I have a doubt, if static members exist on heap then when does the garbage collector sweep them off the heap or it wouldn't consider them?


I think when ever the class is unloaded by the JVM.
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After all the discussion this is what I have concluded till now:
"There exist a separate space for static members (whether reference or variable) on the heap, which is freed when the class is unloaded by the JVM. And this reserved portion of heap for static members is never considered by Garbage Collector for garbage collection."
mohamed sanaullah wrote:
swaraj gupta wrote:But I have a doubt, if static members exist on heap then when does the garbage collector sweep them off the heap or it wouldn't consider them?


I think when ever the class is unloaded by the JVM.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic