When are static members garbage collected?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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 said:
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)?
 
Back
Top