aspose file tools*
The moose likes Beginning Java and the fly likes about static variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about static variable" Watch "about static variable" New topic
Author

about static variable

George Lin
Ranch Hand

Joined: Jan 11, 2005
Posts: 125
Hello everyone,


I have heard that static variable will make the program consume very much memory. It is because garbage collector (GC) will not work on static variable. Is that correct?

I am also wondering when does the memory space of a static variable is allocated, during compile or during runtime?


thanks in advance,
George
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3697
    
    5

Good questions! I'm not positive about my answers to all of your questions, except in two areas. First, if you set the pointer of a static variables to null and the application did not save any previous pointers to that object, then in theory the object would be eligible for garbage collection since there are no valid references to it.

Second, there's no way to guarentee any behavior of the garbage collector other than it will try to run before the system runs out of memory, so even well organized static code may not be garbage collected.


My Blog: Down Home Country Coding with Scott Selikoff
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
The GC collects objects, not references (variables). If an object is not referenced anywhere, it is elibible for GC.

Static variables are initialized when the class is loaded by the JVM. More generally, memory space is allocated during runtime. Memory is allocated for the program when the program is run.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306

I have heard that static variable will make the program consume very much memory.

This is not true always.


In class Lot guestHouse and res1 share the same object for Ventillation_type. So memory allocation will be reduced. If Ventillation type was not Static there could have been an additional Ven_Type object created


This is not correct. In the above code
if you set ventillation_type to null (all refrences), the object referenced by ventillation_type (ie. the object created when new VentType() is executed) will be garbage collected after a while (unpredictable when).


I am also wondering when does the memory space of a static variable is allocated, during compile or during runtime?

Not during compile time. During compilation the House.class will be created. The House.class have an indication that VENTILLATION_TYPE is static variable.

The memory space is allocated at runtime. See the previous reply. Each time the class is loaded the new VentType is executed.

Each time the class is loaded an object of the class 'Class' is created. So for res1 and guestHouse which are two objects of type House there is one more Object. That object is of the type 'Class'. The static variable is held by this 'Class' object.

The 'Class' object for House is created when class 'House' is loaded. Whenever the House is refrenced by an executable statement at runtime the class is loaded.

A few examples of when class can be loaded are

Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
Hi, George -

1. As I'm sure you already got, storage is allocated at runtime (not, as in C/C++, at compile or link times).

2. No, absolutely no, declarating a variable static does NOT "consume very much memory".

A static variable is allocated once, up-front, when the class is first loaded. If you never use it, then sure, you can regard those few bytes as "wasted" (assuming the JVM didn't optimize it away anyway). If you DIDN'T declare it "static", then you'd be consuming even MORE memory - one for each class instance!

3. But that's not really the right way to look at it.

Your ONLY concern should be:

Does this variable pertain to the class as a whole (in which case you should declare it "static"), or can it vary from object instance to object instance (in which case you should *not* declare it static)?

'Hope that helps .. PSM

If


Paul M. Santa Maria, SCJP
George Lin
Ranch Hand

Joined: Jan 11, 2005
Posts: 125
Thanks Scott!


Originally posted by Scott Selikoff:
Good questions! I'm not positive about my answers to all of your questions, except in two areas. First, if you set the pointer of a static variables to null and the application did not save any previous pointers to that object, then in theory the object would be eligible for garbage collection since there are no valid references to it.


Could you show me a simple example about your points? I feel that I am not quite understand your points above.

Second, there's no way to guarentee any behavior of the garbage collector other than it will try to run before the system runs out of memory, so even well organized static code may not be garbage collected.


So, you mean we have no means to verify whether GC will work on static variables?


regards,
George
George Lin
Ranch Hand

Joined: Jan 11, 2005
Posts: 125
Thanks Marilyn,


Originally posted by Marilyn de Queiroz:
The GC collects objects, not references (variables). If an object is not referenced anywhere, it is elibible for GC.

Static variables are initialized when the class is loaded by the JVM. More generally, memory space is allocated during runtime. Memory is allocated for the program when the program is run.


So, your points are GC will not work on static variables since they are variables and not objects. Am I correct?

Maybe I have not made myself understood enough, my question is that whether GC will work on the object pointed by a static variable. Could you help please?


regards,
George
George Lin
Ranch Hand

Joined: Jan 11, 2005
Posts: 125
Thanks Paul!


Originally posted by Paul Santa Maria:

2. No, absolutely no, declarating a variable static does NOT "consume very much memory".

A static variable is allocated once, up-front, when the class is first loaded. If you never use it, then sure, you can regard those few bytes as "wasted" (assuming the JVM didn't optimize it away anyway). If you DIDN'T declare it "static", then you'd be consuming even MORE memory - one for each class instance!


Maybe I have not made myself understood enough, my question is that whether GC will work on the object pointed by a static variable. Since in my mind, a static variable will always available during the lifetime of a Java application (is this point correct?), so the static variable will always be pointed to the object, and finally since there are variables (references) pointing to the object, GC will not work (on the object pointed by the static variable). Could you help please?


regards,
George
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Right, a static variable does not go out of scope. If the only reference to some large object is a static variable it will stay in scope and cannot be collected. Loaded classes can go out of scope under some very carefully constructed circumstances but just figure they are there forever.

By making something static it's possible to make a mistake where you think you're gathering objects in a cache or collection for a short time and you wind up gathering them for the whole run of the program. Any time I see a static collection I think "That could grow forever ... how do we make sure it doesn't?"

And don't blame "static" when that happens. A bug is a bug, and the huge majority of them (or at least mine) are the programmer's fault.
[ November 02, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
George Lin
Ranch Hand

Joined: Jan 11, 2005
Posts: 125
Thanks Stan,


Originally posted by Stan James:
Right, a static variable does not go out of scope. If the only reference to some large object is a static variable it will stay in scope and cannot be collected. Loaded classes can go out of scope under some very carefully constructed circumstances but just figure they are there forever.


- When you say "collected", do you mean garbage collected?

- In what circumstances could "loaded classes can go out of scope"? How to "figure they are there forever"? Could you provide a sample circumstance?

Originally posted by Stan James:
By making something static it's possible to make a mistake where you think you're gathering objects in a cache or collection for a short time and you wind up gathering them for the whole run of the program. Any time I see a static collection I think "That could grow forever ... how do we make sure it doesn't?"[/QB]


What means "you think you're gathering objects in a cache or collection for a short time and you wind up gathering them for the whole run of the program"? Could you provide a simple sample please?


regards,
George
George Lin
Ranch Hand

Joined: Jan 11, 2005
Posts: 125
jiju,


Your reply is great! I have only one more question about it.

Originally posted by jiju ka:

The 'Class' object for House is created when class 'House' is loaded. Whenever the House is refrenced by an executable statement at runtime the class is loaded.


In the above statement, do you mean the class 'House' is loaded again and again when House is referenced by an executable statement?


regards,
George
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about static variable
 
Similar Threads
static variable memory allocation
asking about static n non static memory difference
Serializable statics
static variable
Forward referencing during variable initialization