I have an ASP app which references a few static properties in some of the
classes. I understand that you should use Session variables,
Actually, the problem is that you DON'T understand. A blanket statement like
"you should use Session variables" indicates that you don't understand, but
have heard people say this. Session variables, Static properties and
methods, and other means of storing data in a more or less global fashion
are tools. A good carpenter understand his tools; otherwise he can't use
them properly.
So, let me see if I can help you understand your tools better.
Anything that is static is stored in the heap, rather than the stack. There
are 2 basic memory areas in any applicaiton. The heap is where all the code
for the app is initially loaded. The stack is where instances (copies) of
variables, functions, etc. are kept when they are instantiated. Data in the
stack is volatile; When it goes out of scope, it is removed from the stack.
When you call a function, for example, a copy (instance) of that function is
placed on the stack. When the function exits, it is pulled off the stack.
The heap, on the other hand, is not volatile. It remains in memory for the
lifetime of the app.
Session is an object, or rather, an instance of an object. It is a managed
memory space (Collection) that you can use in a thread-safe manner. Static
data is in the heap, and there is only one instance of it (hence the term
"singleton"). For this reason, it is not thread-safe. It is entirely
possible for more than one thread to be accessing the data at the same time.
Static data is NOT stored in Session. Session is on the Stack; static data
is in the heap. If yoiu PUT a static member into Session, it still remains
in the heap, and the Session variable points to it.
In addition, static data is by nature global to the entire app. Session data
is only global to a single user Session.
And BTW, you also have the Application Cache to store global data in. The
Application Cache is similar to Session, but global to the entire app, and
it is thread-safe. You also have ViewState, which is scoped to a Page.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.