aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static variables" Watch "static variables" New topic
Author

static variables

swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

where do the static variables are stored in java...?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

I'm not exactly sure, but I believe they are stored in an instance of the Class class.
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

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

Joined: Aug 13, 2010
Posts: 46

They are stored in the Class object associated with that class. Instances of Class are also stored on the heap called "permanent generation".


ocpjp 6.0 certified.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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.


Mohamed Sanaulla | My Blog
amit mandal
Ranch Hand

Joined: Aug 13, 2010
Posts: 46

you can read it here even though its bit confusing :

permanent generation
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

amit mandal wrote:you can read it here even though its bit confusing :

permanent generation


Thanks for sharing the article
amit mandal
Ranch Hand

Joined: Aug 13, 2010
Posts: 46

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

Joined: Oct 22, 2010
Posts: 181

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

Joined: Aug 13, 2010
Posts: 46

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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Oct 22, 2010
Posts: 181

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static variables