Question on Page lifecycle...

G

Guest

Hello All,

I have a question which is pertinent to Page's lifecycle.

I declared a protected static object (global variable within that class)
whose value is set only once when the page is loaded. Will that object's
value be accessible
during consequent postbacks or is it set to null after HTML is rendered
initially?

I am assuming that it would be set to null because all the objects are
destroyed since Page object is killed after HTML has been rendered. In this
case ViewState does not make sense because it is an object and not a server
control.....correct?

Am I right or can anyone please explain me the concept?

Thanks a bunch!!!
 
T

Teemu Keiski

Hi,

if you have a static member, it will live as long as the AppDomain
(application) itself. So yes, it is accessible.
 
G

Guest

Dear Teemu,

Thanks for your reply.

How about if I have a global variable not a static global variable. Would
its value be accessible for postbacks?
 
G

Guest

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?

Thanks for clarifying me these concepts!!
 
K

Kevin Spencer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top