简体   繁体   中英

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?

There are four possible root types in .NET Framework:

  • Stack references : references to local objects . Such roots live during a method execution.
  • Static references : references to static objects . These roots live the entire app domain life time.
  • Handles : typically, these are references used for communication between managed and unmanaged code. Such roots must live at least until the unmanaged code needs "managed" objects.
  • Finalizer references : references to objects waiting to be finalized. These roots live until the finalizer is run.

https://www.jetbrains.com/help/dotmemory/Analyzing_GC_Roots.html

High Frequency Heap

Static data and constants defined in a C# program are stored on the heap. Since they exist for the lifetime of the application, they do not need to be garbage collected and are therefore stored in a loader heap, rather than the normal Garbage Collected heap. Specifically, static data is stored on the high-frequency heap–one of the loader heaps that exists for each AppDomain.

Static Data and Constants Are Stored on the Heap 在此处输入图片说明

在此处输入图片说明

在此处输入图片说明 在此处输入图片说明

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.

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.

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.

Instance is created through new keyword and reside in heap that will be garbage collected if no one point it. but in case of static class static constructor invoke only one to initialize memory of all static member that will reside in global memory location that is other than stack and static member remains live unless application is running.it is not garbage collected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM