How Is Memory Allocated for a Static Variable

What is the actual memory place for static variables?

Static fields are initialised when a class is loaded and are discarded when the classloader for that class is unloaded. They can be cleaned up, even duplicated in another class loader.

For applications like those that use OSGi, static variables don't live for the whole life of the application. They can be reloaded many times.

How this is implement may be JVM dependent, but the Sun/Oracle JVM creates an "object" to hold the static fields for a class. This object is accessible via the Unsafe class which can also be used to examine this "objects" fields.

When is the memory allocated for a static variable in java?

When the class is loaded, at runtime. You can find the details here.

Memory allocation to static variables (Compile time memory allocation)

You've misunderstood the statement 'memory allocation at compile time'. What is meant by that is that the compiler writes data to the binary that it produces that indicates that the memory should be set aside when the program is loaded by the operating system.

In particular, the field is usually stored in a section in the output file that is called the BSS. The compiler puts the static variable declaration in the BSS, the OS's program loader reads out the BSS section when loading the program, and sets aside enough memory in the freshly created process to store the BSS.

Every time the program is launched, that is every time a new process is created, new memory is set aside for that process. This includes the memory needed for the BSS aka static variables.

When is memory allotted to static variables in C++

First: STOP THINKING ABOUT GLOBAL VARIABLES IN C AND C++, or you will perpetuate your state of confusion. The issue is more complex than in, e.g., Python or Pascal, and therefore you can't just use a single word for the concept(s).

Second, there is no "heap" or "stack" -- these are operating system and CPU details, and have nothing to do with the abstract C++ language specification.

Now, variables have 1) scope, 2) linkage and 3) storage class. The static keyword is used to affect the all three aspects, depending on where you use it.

Scope: where the variable is declared. If within a function, it is a function-scope, if outside of a function, it is file-scope (what you probably refer to as "global variable").

Linkage: whether the variable is accessible from other compilation units (relevant when your program consists of multiple source files).

Storage class:

Static variables are allocated in implementation-defined manner upon program startup, and they live until the program ends. They cannot be "freed" or "reallocated". (a typical implementation are BSS and DATA segments as others have mentioned).

Automatic variables exist only at function scope, they are allocated (and possibly initialized) on function entry (usually on the CPU's stack) and deallocated on function exit.

Dynamic storage class is what you probably refer to "the heap". Those are variables whose storage is directly manipulated through malloc/free or new/delete. Storage for static variables is managed in a VERY different way than dynamic storage, and the two kinds of storage are fundamentally incompatible.

Example:

===
// ALL file-scope variables have static storage class
int global1; // file-scope, external linkage
static int global2; // file-scope, internal linkage

void f()
{
static int x; // static storage class, function-scope
int y; // automatic storage class, function-scope

free(&x); // ILLEGAL, WILL CRASH!
free(&y); // DITTO!
free(&global1); // DITTO!
}
===

Now, all variables with static storage class (global1, global2 and x) are allocated and initialized before program startup. If you do not specify initial values, they are default-initialized in UNSPECIFIED ORDER. (For primitive types, this just means filling with zeros).

Static variables are deallocated only at program exit. This means, that 'x' in function f will be initialized ONLY ONCE (at program startup) and that it will RETAIN ITS VALUE between invocations of the function (as opposed to y which will be allocated on each function entry and deallocated at each function exit, thus also its value destroyed). Note that using static variables within functions is very incompatible with multithreading and reentrancy, unles you know very well what you're doing.

How is memory allocated for a static variable?

Memory for static variables are normally held in some rooted (and hidden) object[]. This can be seen doing a !gcroot on the object in WinDbg (with SOS).

Just to add, these references can never be GC'ed (unless you null the field), as I discovered recently.



Related Topics



Leave a reply



Submit