Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static class memory allocation where it is stored C#

I read an article which confused me about memory allocation, which stated:

Singleton objects are stored on the heap while static classes are stored on the stack.

link is : http://www.dotnetjalps.com/2013/06/Static-vs-Singleton-in-Csharp-Difference-between-Singleton-and-Static.html

But in some Stackoverflow questions, such as

How is memory allocated for a static variable?

It was described like

Static variables are stored on the heap, regardless of whether they are declared as a reference type or a value type. There is only one slot in total no matter how many instances are created.

So I am confused with stack or heap storage for static classes. How is memory allocated for a static class and why? How is memory allocated for singleton class?

like image 858
SivaRajini Avatar asked Nov 06 '15 08:11

SivaRajini


People also ask

What is static memory allocation in C?

1. Static Memory Allocation As we discussed static memory allocation is the allocation of memory for the data variables when the computer programs start. This type of allocation is applied to only global variables, file scope variables and also to those variables that are declared as static.

What is the difference between stack memory and static memory?

All local variables are stored in stack memory at compile-time and all pointer variables are stored in heap memory at the runtime of a program In static memory allocation, memory allocated is fixed which cannot be changed or altered after allocation.

Where are static variables stored in the memory?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program.

Are classes stored in stack or memory?

15 Classes will not take memory but objects do. The statement "static class stored in stack" sounds absurd to me. Classes are not stored in memory. When a class is loaded, their metadata may be loaded in memory and cached. Apart from that classes are not stored in memory.


3 Answers

Classes will not take memory but objects do. The statement "static class stored in stack" sounds absurd to me.

Classes are not stored in memory. When a class is loaded, their metadata may be loaded in memory and cached. Apart from that classes are not stored in memory.

Question yourself that if static classes were stored in stack, how can you able to access it in all threads?

Static Variables

Static variables are an important constituent part of the MethodTable data structure. They are allocated as a part of the MethodTable right after the method table slot array. All the primitive static types are inlined while the static value objects like structs and reference types are referred through OBJECTREFs created in the handle tables. OBJECTREF in the MethodTable refers to OBJECTREF in the AppDomain handle table, which refers to the heap-created object instance. Once created, OBJECTREF in the handle table will keep the object instance on the heap alive until the AppDomain is unloaded

Refer this article for more info

Please stop reading that blog post or any blog posts from that author. It is utterly absurd.

like image 86
Sriram Sakthivel Avatar answered Sep 24 '22 09:09

Sriram Sakthivel


Nice explained by Sriram Sakthivel. Basically the Heap memory is divided into 2 major parts. Object heap memory and Loader heap memory. As per my understanding All non static reference type are stored on object heap and all the static object(may be it is reference type or value type) are stored in loader heap. Gc never work on loader heap thats why they initilized only once and remain in memory throught the application.

like image 22
Red Swan Avatar answered Sep 23 '22 09:09

Red Swan


Static variable goes to the special reason within Heap.It is called High Frequency Heap , all the static variables go to the High Frequency Heap in memory. Objects in High Frequency Heap is not garbage collected by GC and hence static variables available throughout life time of an application.

We need to explicitly de-allocate it then we have to set it to null so that GC can clear it's allocated memory.

like image 39
Sheo Dayal Singh Avatar answered Sep 21 '22 09:09

Sheo Dayal Singh