What is the behavior of static variables no.NET?

What is the behavior of static variables in .NET? are they stored in the heap or in the stack?

Author: Maniero, 2014-08-04

2 answers

Static data, not only variables, constants and codes as well, is stored in a special heap, called the High Frequency Heap. It is an area managed by .NET, but its content is not collected. There is stored what the application needs for its entire life. Static data is stored there from its first use until the application terminates, unlike normal heap where data is collected when there are no more references to it at the moment that one of the generations shoots a collection.

There is actually a collection of this area when a AppDomain is unloaded. What does not cease to be the end of the application in a sense. Surely at the end of AppDomain you will no longer be able to access this data. The general allocation of this memory area occurs at the load of AppDomain. Mo .NET Core is no longer AppDoamin.

Static data is never in the normal stack or heap. It would not make sense since these data are not transitory (to better understand these areas read what they are and where the stack and heap are). The HFH special area is within the heap but has a special treatment. Of course static data (variables, codes, etc.) can refer to the stack or heap.

It is important to note that same data by value that would normally be in the stack or in the heap (See more in memory allocation in C# - Value Types and types reference ) are stored in this area when they are static.

Note that if a static variable is a type by reference only the reference will be in this area, the object itself will be in the normal heap . It has exceptions, such as the striung which was defined by a literal.

A literal of type string is a static data and it is stored only once even if it appears multiple times in the code through a technique called interning .

It is worth remembering that a static object is allocated only once and is not relocated by definition.

The jitter uses this same area to allocate the generated codes.

Of course this is all implementation detail. Nothing prevents it from being changed in future versions, although it is unlikely to happen. In fact the importance of this information for the programmer is small. The most important thing is to know that they are stored very quickly (from a overall way comparable with the time spent on the stack or a little better) and as it does not dislodge nor release, there is no other cost.

 9
Author: Maniero, 2020-09-07 15:51:57

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 the Heap (or static), and local variables are stored in the Stack (stack, automatic).

Stack is the execution stack. Each executed method is placed on the stack. In this way, the code of the methods and local variables (including parameters) are placed in the Stack . If a method fills the Stack, the StackOverflowException exception is thrown; this means that the stack has grown so much that it has invaded an area that does not belong to it.

Heap is the place where instantiated objects are located, that is, it is where instance variables and references (objects) are placed. When a method creates too many instance variables to the point of filling the 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: http://kamilladoria.blogspot.com.br/2010/05/stack-e-heap.html

 1
Author: Dante, 2014-11-18 10:58:06