I know we dont initialize a static class. We can use them by class name. But it has to be in the Static Memory so that we can use them. When does a Static Class get placed in Static Memory? Is it on RunTime or Lazy Loading?
-
1Did you check this: https://msdn.microsoft.com/en-us/library/79b3xss3.aspx ? – shree.pat18 Feb 12 '15 at 06:30
-
A static class just "exists". All fields it declare are given space in memory when the assembly is first loaded. Until the static constructor is executed, however, the state and values of those fields are not initialized. – Lasse V. Karlsen Feb 12 '15 at 06:49
-
I have no idea what you think "static memory" is, nor why a static class has to be placed in it. – Damien_The_Unbeliever Feb 12 '15 at 07:26
1 Answers
I suspect you're thinking of it as lazy. I don't know what you mean by "static memory," but I think under that is a very viable question about the workings of static members and how they play with memory. Static members will be placed in "regular memory," just like any instance members.
Static instances are placed onto the heap at load-time (although it is a different kind of heap than instance ones, called the High Frequency Heap), but are not initialized until use. Depending on which static members you have (and namely their types, and whether those types are reference or value types), this can be a big or a small deal.
For instance, if you have a byte array with 200 elements, that will be loaded only when you use it (less the null
that it will initially hold). But if you have 200 byte elements, each will be placed into memory at the beginning.
In other words, before use, ClassA
will take up less memory than ClassB
, but during use, they'll be similar. Not exactly the same, of course, but you can imagine the point I'm trying to make.
static class ClassA
{
public static byte[] bytes = new byte[] { 1, 12 };
}
static class ClassB
{
public static byte ByteA = 1;
public static byte ByteB = 12;
}
That's because in ClassA
, the initial value of bytes
is null
, but in ClassB
, there are two values stored from the process's start, both of which happen to be 0
. You can imagine how this might play into things with other reference- versus value-typed fields.
Another difference comes into play when the GC runs. If I'm not mistaken (although I don't believe this is spec'd behavior), static members aren't garbage collected until the process is terminated. Other than that, though, you can (loosely) think of them as a simple, compiler-enforced singleton instance of the class.
The easiest way to determine this "lazy loading" of values would be to place a side-effecting call in a static constructor. Something like,
public class Foo
{
static Foo()
{
Console.WriteLine("In static constructor!");
}
public static void Bar()
{
Console.WriteLine("In Bar!");
}
}
Put in the context of a small console app, you could easily determine when the constructor is called, and infer that that, too, is when static members are placed into memory.
void Main(string[] args)
{
Console.WriteLine("First line!");
Foo.Bar();
Console.WriteLine("Last line!");
}
This program outputs:
First line!
In static constructor!
In Bar!
Last line!
You can also find this information in the C# 5.0 spec (§10.12 Static Constructors),
The static constructor for a closed class type executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:
- An instance of the class type is created.
- Any of the static members of the class type are referenced.
In laymen's terms, the static members are initialized the first time you use the static class. If it's never used, they're never initialized.
Just to drive my point home, let's look at a final pair of methods.
static class Foo
{
public static int X = 2;
public static byte[] Bytes = new byte[] { 24, 66 };
}
class Bar
{
public int X = 2;
public byte[] Bytes = new byte[] { 24, 66 };
}
Stepping through (at an IL-level), the interactions with these classes would be pretty different.
Consider a method:
void Main(string[] args)
{
// Foo is put into memory when the process spins up:
// X : 0
// Bytes : null
Console.Write(Foo.X); // During this line, Foo is initialized
// X : 2
// Bytes : 24, 66
Console.Write(Foo.X); // Nothing happens
Bar b = new Bar(); // During this line, Bar is put into memory, then initialized
// X : 0, then "instantaneously" becomes 2
// Bytes : null, then "instantaneously" becomes 24, 66
Console.Write(b.X); // Nothing happens
// Eventually, b will be cleared from memory by the garbage collector
// but Foo will not be, until the program closes. This sample is much
// too small to display that difference.
}

- 1
- 1

- 12,916
- 5
- 38
- 54
-
Static Memory is synonym of HFH : Whenever a process is loaded in the RAM, we can say that the memory is roughly divided into three areas (within that process): Stack, Heap, and Static (which, in .NET, is actually a special area inside Heap only known as High Frequency Heap) http://www.codeproject.com/Articles/15269/Static-Keyword-Demystified – kibs Feb 13 '15 at 03:50