Question on Page lifecycle...

Discussion in 'ASP .Net' started by =?Utf-8?B?RGlmZmlkZW50?=, Feb 28, 2006.

  1. 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!!!
     
    =?Utf-8?B?RGlmZmlkZW50?=, Feb 28, 2006
    #1
    1. Advertising

  2. =?Utf-8?B?RGlmZmlkZW50?=

    Teemu Keiski Guest

    Hi,

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

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    "Diffident" <> wrote in message
    news:...
    > 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!!!
     
    Teemu Keiski, Feb 28, 2006
    #2
    1. Advertising

  3. 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?

    "Teemu Keiski" wrote:

    > Hi,
    >
    > if you have a static member, it will live as long as the AppDomain
    > (application) itself. So yes, it is accessible.
    >
    > --
    > Teemu Keiski
    > ASP.NET MVP, AspInsider
    > Finland, EU
    > http://blogs.aspadvice.com/joteke
    >
    >
    > "Diffident" <> wrote in message
    > news:...
    > > 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!!!

    >
    >
    >
     
    =?Utf-8?B?RGlmZmlkZW50?=, Feb 28, 2006
    #3
  4. 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!!
    "Teemu Keiski" wrote:

    > Hi,
    >
    > if you have a static member, it will live as long as the AppDomain
    > (application) itself. So yes, it is accessible.
    >
    > --
    > Teemu Keiski
    > ASP.NET MVP, AspInsider
    > Finland, EU
    > http://blogs.aspadvice.com/joteke
    >
    >
    > "Diffident" <> wrote in message
    > news:...
    > > 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!!!

    >
    >
    >
     
    =?Utf-8?B?RGlmZmlkZW50?=, Feb 28, 2006
    #4
  5. > 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.




    "Diffident" <> wrote in message
    news:...
    >
    > 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!!
    > "Teemu Keiski" wrote:
    >
    >> Hi,
    >>
    >> if you have a static member, it will live as long as the AppDomain
    >> (application) itself. So yes, it is accessible.
    >>
    >> --
    >> Teemu Keiski
    >> ASP.NET MVP, AspInsider
    >> Finland, EU
    >> http://blogs.aspadvice.com/joteke
    >>
    >>
    >> "Diffident" <> wrote in message
    >> news:...
    >> > 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!!!

    >>
    >>
    >>
     
    Kevin Spencer, Mar 1, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Epson Barnett
    Replies:
    1
    Views:
    353
    Epson Barnett
    Apr 21, 2004
  2. John Lau

    Page and control lifecycle

    John Lau, May 11, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    542
    John Lau
    May 11, 2004
  3. Frankie
    Replies:
    2
    Views:
    6,025
    Frankie
    Jun 23, 2005
  4. =?Utf-8?B?RGlmZmlkZW50?=

    Question on Page lifecycle...

    =?Utf-8?B?RGlmZmlkZW50?=, Jan 10, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    441
    Eliyahu Goldin
    Jan 11, 2006
  5. Gary W. Smith

    Page lifecycle, specific events question.

    Gary W. Smith, Aug 28, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    833
    bruce barker
    Aug 28, 2007
Loading...

Share This Page