Since every new request is served by a new thread how would .NET
framework
persist this static values? Where I they stored in AppDomain? And where
are
the normal variables and global variables stored in the AppDomain?
Remember that a thread is not a class. Nor is it the programming heap. The
programming heap is created when the Application starts, and remains there
for the lifetime of the Application. All classes are stored in the heap when
they are loaded. A thread is simply a process that makes use of the
programming heap (and the programming stack). The difference is that an
instance class has multiple copies in the heap, while a static class has
only one.
Where things are stored depends upon what sorts of things they are. I'm
going to back up a bit and explain the heap and stack first. The heap is a
long-term memory storage area. The stack is a short-term memory storage
area. All reference types are stored in the heap. All primitives and value
types are stored in the stack. A reference type is a type that is not
accessed directly, but by means of pointers. A pointer is a special (value
type) variable that, in essence, stores the memory address of something
else. Pointers are placed on the stack, along with other primitives and
value types, like integers, structures, etc. The stack is sort of like a
scratch pad that a thread uses to keep track of things. The heap remains in
memory for the lifetime of the Application. So, when a thread creates an
instance of a class, a copy of that class is created in the heap, and a
pointer to that class is placed on the Stack. When you call a method in a
class, a copy of that method code is placed on the stack, and it executes.
If it changes anything in the class it belongs to, the change occurs in the
class instance in the heap. When you work with an instance of a class, you
are working with a pointer in the Stack, which points to that instance of
the class in the heap.
This is why Garbage Collection exists. You may have an instance of a class
in the heap which is no longer being used. That is, there are no other class
instances that reference it, and there are no pointers on the stack that
reference it. The class, however, is still there, in the heap. The Garbage
Collection in .Net patrols the heap, looking for such "orphaned" classes,
and cleans them up, eventually.
As for "global variables," that is a term that is not very descriptive of
anything. It could refer to any number of things, so I can't specifically
answer that question. "global" is a reference to "scope" which is somewhat
relative. For example, a variable can be scoped "globally" within a class.
In terms of an Application, there are any number of ways to store data
"globally."
But all of these things follow the same general rules I laid out for you
above. Here are a couple of articles that go into more detail about all of
this:
http://www.yoda.arachsys.com/csharp/memory.html
http://www.codeproject.com/managedcpp/garbage_collection.asp
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.