How to store objects in application state?

Discussion in 'ASP .Net' started by Mel, Sep 29, 2004.

  1. Mel

    Mel Guest

    This may be a stupid question, but here goes...

    I have created a NameValueCollection in my website's application state. If,
    during a page request, I add a string key and string value to the
    collection, how are they stored? Strings are objects so I'm really only
    passing references, right? Now I assume that request handlers run in
    different threads with their own memory spaces, and when the request ends,
    the memory space is unaloted, taking with it my string objects. What am I
    missing here?
    Mel, Sep 29, 2004
    #1
    1. Advertising

  2. Hi Mel,

    A point you are missing can be the fact that the application state bag is
    not deallocated when the request ends. The application state bag (referenced
    by this.Application) is active as long as the application runs and it's an
    application wide storage area.

    When you pass a string to the application object, the reference of the
    string object is passed so you are right on that. When the application
    shutdowns, then this reference is gone and at this moment, the string is now
    garbage. Since string is a managed type, it's garbage collected
    automatically. Coming back to the namevalue collection, your application
    state bag keeps a reference to the namevalue collection on the memory. All
    other objects kept inside the namevalue collection are referenced through
    this reference. Namevalue collection simply keeps reference to objects that
    you store in it. So once this reference to the namevalue is gone, so are the
    references to other objects.

    In general, there are levels of storage options in ASP.NET. You can store
    variables on the page level, on the session level and on the application
    level. All of them work similiarly as described above, with the exception of
    when they are destroyed.

    When you create the string on the page, you get a reference and this
    reference is destroyed when the page object is deallocated. But, since you
    add the string also to the namevalue collection, which is active in the
    Application state bag, another reference is still pointing at it. So it's not
    lost, hence it's not deallocated.

    Ethem Azun

    "Mel" wrote:

    > This may be a stupid question, but here goes...
    >
    > I have created a NameValueCollection in my website's application state. If,
    > during a page request, I add a string key and string value to the
    > collection, how are they stored? Strings are objects so I'm really only
    > passing references, right? Now I assume that request handlers run in
    > different threads with their own memory spaces, and when the request ends,
    > the memory space is unaloted, taking with it my string objects. What am I
    > missing here?
    >
    >
    >
    =?Utf-8?B?RXRoZW0gQXp1bg==?=, Sep 29, 2004
    #2
    1. Advertising

  3. Mel

    Mel Guest

    Thanks Ethem,

    so basically object lifetime issues are the same across threads as they are
    within a thread. The main thing I have to deal with is concurrent access to
    shared objects. To do this I use such things as C#'s lock() mechanism, and
    the synchronised wrappers available for some intrinsic classes such as
    Hashtables.

    "Ethem Azun" <> wrote in message
    news:...
    >
    > Hi Mel,
    >
    > A point you are missing can be the fact that the application state bag is
    > not deallocated when the request ends. The application state bag
    > (referenced
    > by this.Application) is active as long as the application runs and it's an
    > application wide storage area.
    >
    > When you pass a string to the application object, the reference of the
    > string object is passed so you are right on that. When the application
    > shutdowns, then this reference is gone and at this moment, the string is
    > now
    > garbage. Since string is a managed type, it's garbage collected
    > automatically. Coming back to the namevalue collection, your application
    > state bag keeps a reference to the namevalue collection on the memory. All
    > other objects kept inside the namevalue collection are referenced through
    > this reference. Namevalue collection simply keeps reference to objects
    > that
    > you store in it. So once this reference to the namevalue is gone, so are
    > the
    > references to other objects.
    >
    > In general, there are levels of storage options in ASP.NET. You can store
    > variables on the page level, on the session level and on the application
    > level. All of them work similiarly as described above, with the exception
    > of
    > when they are destroyed.
    >
    > When you create the string on the page, you get a reference and this
    > reference is destroyed when the page object is deallocated. But, since you
    > add the string also to the namevalue collection, which is active in the
    > Application state bag, another reference is still pointing at it. So it's
    > not
    > lost, hence it's not deallocated.
    >
    > Ethem Azun
    >
    > "Mel" wrote:
    >
    >> This may be a stupid question, but here goes...
    >>
    >> I have created a NameValueCollection in my website's application state.
    >> If,
    >> during a page request, I add a string key and string value to the
    >> collection, how are they stored? Strings are objects so I'm really only
    >> passing references, right? Now I assume that request handlers run in
    >> different threads with their own memory spaces, and when the request
    >> ends,
    >> the memory space is unaloted, taking with it my string objects. What am I
    >> missing here?
    >>
    >>
    >>
    Mel, Sep 29, 2004
    #3
    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. =?Utf-8?B?UnVkeQ==?=

    to store or not to store an image

    =?Utf-8?B?UnVkeQ==?=, Mar 29, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    624
    =?Utf-8?B?UnVkeQ==?=
    Mar 30, 2005
  2. =?Utf-8?B?RXJpYyBNY1ZpY2tlcg==?=

    Why can't Application state be managed like Session state?

    =?Utf-8?B?RXJpYyBNY1ZpY2tlcg==?=, Nov 7, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    1,537
    Steven Cheng[MSFT]
    Nov 10, 2005
  3. Replies:
    6
    Views:
    464
    John Timney \( MVP \)
    Nov 30, 2005
  4. Derek Basch
    Replies:
    4
    Views:
    179
    -berlin.de
    Aug 16, 2006
  5. Replies:
    7
    Views:
    374
    David Lamb
    Oct 29, 2012
Loading...

Share This Page