What is the behavior of static variables in .Net? Are these stored in the Heap or the Stack?
What is the behavior of static variables in .Net? Are these stored in the Heap or the Stack?
Static data, not only variables, constants and codes as well, are stored in a special heap , called High Frequency Heap. It is an area managed by .NET, but its content is not collected. There is stored what the application needs throughout its life. Static data is stored there from its first use until the application finishes, unlike the normal heap where the data is collected when there are no more references to them the moment one of the generations fires a collection. p>
There is actually a collection of this area when a AppDomain
is unloaded. This is still the end of the application in a sense. Certainly at the end of AppDomain
you will no longer be able to access this data. The general allocation of this memory area occurs at AppDomain
load.
Static data is never in the stack or heap normal. It would not make sense since these data are not transient (to better understand these areas read What are and where are the stack and heap ). The special HFH area is within heap but has a special treatment. Of course, static data (variables, codes, etc.) may refer to the stack or heap
It's important to note that even value data that would normally be in or heap (see Memory Allocation in C # - Value Types and Reference Types ) are stored in this area when they are static.
Note that if a static variable is a reference type only the reference will be in this area, the object itself will be in the normal heap . There are exceptions, such as the striung that was defined by a literal.
A literal of type string is a static data and it is stored only once even though it appears several times in the code through a technique called interning .
Note that a static object is allocated only once and is not reallocated by definition.
JITter uses this same area to allocate generated codes.
Of course, this is implementation detail. Nothing prevents future versions from being changed, though unlikely to happen. In fact the importance of this information to the programmer is small. The most important thing is to know that they are stored very quickly (generally comparable to the time spent in the stack or a little better) and since it does not deallocate or release, there is no other cost. p>
The parts of a Java program (variables, methods, and objects) live in one of two places in memory: Stack and Heap .
Instance variables and objects are stored in Heap (or static), and local variables are stored in Stack (stack, automatic).
Stack is the execution stack. Each executed method is placed on the stack. That way, the code of local methods and variables (including parameters) are placed in Stack . If a method populates the Stack , the StackOverflowException
exception will be thrown; this means that Stack has grown so much that it invaded an area that does not belong to it.
Heap is the place where instantiated objects are, that is, where instance variables and references (objects) are placed. When a method creates many instance variables to the point of filling Heap , the OutOfMemoryError
exception is thrown.
import java.util.Date;
public class Agenda {
//variável de instância - heap
Date data;
//método - stack
public static void main(String[] args) {
//variável local - stack
Date dt = new Date();
Agenda agenda = new Agenda();
agenda.setData(dt);
}
//parâmetro - stack
public void setData(Date d) {
data = d;
}
}
Source: link