When are static members garbage collected?

Discussion in 'ASP .Net' started by =?Utf-8?B?Sg==?=, Feb 25, 2004.

  1. I have an object that I am using in my ASP.net app. I only want one instance of this object available to all of my pages.

    Here is a sample

    public class statictes

    private statictest() {} // static onl

    private static string data = null

    public static string Dat

    ge

    if(data != null) return data
    data = "INIT"
    return data

    se

    data = value




    I am assuming that this private static member will no be garbage collected as long as the application is running. Is this correct? Would it be better to store it in cache (I should not expire until application end, and it does not have any dependencies)

    Thank
     
    =?Utf-8?B?Sg==?=, Feb 25, 2004
    #1
    1. Advertising

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

    bruce barker Guest

    statics are GC'd when the appdomain is unloaded. (life of the application).

    special note: your sample code is not thread safe.


    -- bruce (sqlwork.com)




    "J" <> wrote in message
    news:...
    > I have an object that I am using in my ASP.net app. I only want one

    instance of this object available to all of my pages.
    >
    > Here is a sample:
    >
    >
    > public class statictest
    > {
    > private statictest() {} // static only
    >
    > private static string data = null;
    >
    > public static string Data
    > {
    > get
    > {
    > if(data != null) return data;
    > data = "INIT";
    > return data;
    > }
    > set
    > {
    > data = value;
    > }
    > }
    > }
    >
    > I am assuming that this private static member will no be garbage collected

    as long as the application is running. Is this correct? Would it be better
    to store it in cache (I should not expire until application end, and it does
    not have any dependencies)?
    >
    > Thanks
    >
     
    bruce barker, Feb 25, 2004
    #2
    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. Cheng Thao
    Replies:
    0
    Views:
    400
    Cheng Thao
    Aug 6, 2003
  2. Mark McKay
    Replies:
    5
    Views:
    897
    xarax
    Oct 3, 2003
  3. =?Utf-8?B?VmxhZGlzbGF2IEtvc2V2?=

    Static class members disappearing (GC collected?!)

    =?Utf-8?B?VmxhZGlzbGF2IEtvc2V2?=, May 15, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    425
    =?Utf-8?B?VmxhZGlzbGF2IEtvc2V2?=
    May 16, 2006
  4. Scott Robinson

    Socket being garbage collected too early

    Scott Robinson, Dec 16, 2004, in forum: Python
    Replies:
    4
    Views:
    420
    Scott Robinson
    Dec 18, 2004
  5. Kent Johnson

    When is a thread garbage collected?

    Kent Johnson, Mar 16, 2005, in forum: Python
    Replies:
    1
    Views:
    449
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Mar 16, 2005
Loading...

Share This Page