Static Data and Constants Are Stored on the 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.
You might be heard that, reference objects are stored in heap:).
Do you know how many types of heaps are available in CLR.
Do you know that, you can pass data to another app domain with out marshaling:)
These all are .NET CLR implementation details, know to Bill Gates:)
You can explore, if you want to know more, Later i will post about memory management in CLR.
For the time being have a look at the types of heap’s below.
Loader Heap: contains CLR structures and the type system
High Frequency Heap: statics, MethodTables, FieldDescs, interface map
Low Frequency Heap: EEClass, ClassLoader and lookup tables
Stub Heap: stubs for CAS, COM wrappers, PInvoke
Large Object Heap: memory allocations that require more than 85k bytes
GC Heap: user allocated heap memory private to the app
JIT Code Heap: memory allocated by mscoreee (Execution Engine) and the JIT compiler for managed code
Process/Base Heap: interop/unmanaged allocations, native memory, etc
Usually, the “heaps” that most people refer to or know about are the “GC Heap” and the LOH.
GC Heap is shared among app-domains and GC will work only on that.
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.
오브젝트 힙 메모리 및 로더 힙 메모리. 내 이해에 따라 모든 비 정적 참조 유형은 객체 힙에 저장되고 모든 정적 객체 (참조 유형 또는 값 유형일 수 있음)는 로더 힙에 저장됩니다.
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.
정적 변수는 Heap 내의 특별한 이유로 이동하며, High Frequency Heap이라고하며 모든 정적 변수는 메모리의 High Frequency Heap으로 이동합니다.
고주파 힙의 개체는 GC에서 가비지 수집되지 않으므로 응용 프로그램의 수명 기간 동안 정적 변수를 사용할 수 있습니다.
명시 적으로 할당을 해제해야하며 GC가 할당 된 메모리를 지울 수 있도록 null로 설정해야합니다.
Memory Management of Static variables in .Net
We have seen that the .Net run-time manages the variables and objects in Stacks and Heaps based on their type. Value types are stored in the Stack and reference types are stored in the Heap. This is fine for normal variables, but what happens to static variables.
Static variables cannot be treated like other variables, because static variables and methods in a static class can be accessed directly without creating instances of the class. In this post we shall see on how the .net run-time does memory management for static variables.
The lifetime of a static variable cannot be defined exactly, since it can be called anytime from anywhere just by prefixing the variable with the class name, so these variables should be available at any time of the application execution.
Since static variables have the above requirements they cannot be stored in the stack, as the stack frame will get popped up once the method execution is completed, to overcome this .Net run-time stores the static variables in a special region within the Heap called as High Frequency Heap.
Each App Domain in the .Net run-time has a set of Loader Heaps, which contains High-Frequency Heap, Low-Frequency Heap, and Stub Heap. Objects in the High-Frequency Heap are not Garbage collected, this makes sure that the Static variables are available throughout the life time of the application. If we want to explicitly de-allocate a Static variable then we should set it to null, so that the Garbage collector can clear the memory allocated to the static variable.
참고:
csharp.2000things.com/2011/01/03/200-static-data-and-constants-are-stored-on-the-heap/
#200 – Static Data and Constants Are Stored on the 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…
csharp.2000things.com
vivekcek.wordpress.com/tag/loader-heap/
Loader Heap – A day with .Net
Posted by vivekcek on July 10, 2016 You might be heard that, reference objects are stored in heap:). Do you know how many types of heaps are available in CLR. Do you know that, you can pass data to another app domain with out marshaling:) These all are .NE
vivekcek.wordpress.com