I know nonstatic variables are released when variables are in out of scope, but scope for static variables are in all context.
Asked
Active
Viewed 1,575 times
2
-
1Look at [this question](http://stackoverflow.com/questions/11134686/static-variables-what-is-their-life-span) and [this answer](http://stackoverflow.com/a/8704858/364056). – enzom83 Aug 22 '15 at 13:01
-
1possible duplicate of [Does the garbage collector work on static variables or methods in java?](http://stackoverflow.com/questions/13126833/does-the-garbage-collector-work-on-static-variables-or-methods-in-java) – Piotr Siupa Aug 22 '15 at 13:07
2 Answers
3
Static variable's memory is allocated at the start of the program, in regular memory, instead of the stack (memory set aside specifically for the program). The advantage of this is that it makes your variable or procedure totally constant, and you can't accidentally change the value. the disadvantage of this is that the memory is not deallocated until the program is terminated. I have never heard anything that static values take any more memory than if they are declared regularly, but thier memory use is constant through.

krishnas
- 46
- 3
2
Since static classes/members loads once per class loader, when the class loader is eligible for GC, the static members also eligible for GC.

Suresh Atta
- 120,458
- 37
- 198
- 307
-
1For simple programs, the default class loader is not unloaded, but in complex programs with multiple class loaders this does happen. +1 – Peter Lawrey Aug 22 '15 at 12:58
-
@PeterLawrey inshort, *if we configure/have multiple class loaders* :) – Suresh Atta Aug 22 '15 at 12:59
-
-
-
Ahh sorry, I mean static members are always in memory while application is running. – Amol Patil Aug 22 '15 at 13:13
-
-
@totaloverdose while the class loader is still be used, which for a simple program is the life of the application. – Peter Lawrey Aug 22 '15 at 13:17