Static Variable in C# – Exhaustively

Story Starts – When CLR executes the first line of code the managed code. It creates 3 Application Domain. –

  1. System Domain
  2. Shared Domain
  3. Default Domain

Default Domain is an instance of AppDomain within which application code is typically executed.

Each AppDomain has its own SecurityDescriptor, SecurityContext, and DefaultContext, as well as its own loader heaps (High-Frequency Heap, Low-Frequency Heap, and Stub Heap), Handle Tables (Handle Table, Large Object Heap Handle Table), Interface Vtable Map Manager, and Assembly Cache.

LoaderHeaps are meant for loading various runtime CLR artifacts and optimization artifacts that live for the lifetime of the domain. LoaderHeaps are different from the garbage collector (GC) Heap.

GC Heap hosts object instances while LoaderHeaps hold together the type system.

Frequently accessed artifacts like MethodTables, MethodDescs, FieldDescs, and Interface Maps get allocated on a HighFrequencyHeap, while less frequently accessed data structures, such as EEClass and ClassLoader and its lookup tables, get allocated on a LowFrequencyHeap. The StubHeap hosts stubs that facilitate code access security (CAS), COM wrapper calls, and P/Invoke.

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.

StaticVariableVikas Jindal


Static Variable in C#

Each time when we create a new instance of a class we get a new copy of the instance data to play with.

The instance methods of the class work on the instance data.  The instance data is completely independent of the instance data in all other classes.  If we change a value in one instance it has no impact on the same value in other instances.

Static data is equivalent to global data.

Everybody in the program sees the same data. If someone changes the data then everybody else will see the change as well.

When you create an instance of a class you are effectively allocating some memory to hold your own copy of the instance data defined by the class.  If you create 5 instances of a class then you get 5 separate memory locations where each location has its own copy of the instance data.  Each memory block is independent of the others.

There is only one copy of static data in memory in general. Static variables are stored on the Managed Heap, not the Stack, when the type is first referenced.  The Type Object of the compiled class contains a reference to the object. (This heap is separate from the normal garbage collected heap – it’s known as a “high frequency heap”, and there’s one per application domain.)

The Type Object of a class will stay in memory until the AppDomain where it resides is unloaded.  Since the object on the Heap is always being referenced by the compiled Type Object, static objects on the Heap will never by GC’ed and will always consume memory until the AppDomain is unloaded.

Vikas Jindal

HTML5 Questions – Part 1

Some basic questions of HTML5

  1. From where HTML5 originates?
  2. What is the full form of SGML?
  3. What is element in HTML5?
  4. Is HTML5 case sensitive?
  5. What does W3C recommend for tag names?
  6. What is attribute in HTML5?
  7. What is boolean attribute in HTML5?
  8. What is global attribute in HTML5?
  9. What is self-closing tag in HTML5?
  10. What is void element in HTML5?
  11. What is expando attribute in HTML5?

By Vikas Jindal