473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

When are static members garbage collected?

J
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

Nov 18 '05 #1
1 1808
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" <an*******@discussions.microsoft.com> wrote in message
news:B0**********************************@microsof t.com...
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

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lester | last post by:
I have a low level question . . . If I have an assembly with a single class with a static int property, I know the assembly will get loaded the first time the class is referenced. At that...
3
by: Adam Smith | last post by:
Hi. I have a static arraylist in a class which is populated during the class's static constructor. Will this arraylist or the class objects allocated on it ever be garbage collected if I don't...
16
by: Ed Sutton | last post by:
I use a mutex to disallow starting a second application instance. This did not work in a release build until I made it static member of my MainForm class. In a debug build, first instance got...
1
by: Chris Bardon | last post by:
I'm working on a class library in .net, and am having a problem that I can only describe as a memory leak (which garbage collection was supposed to eliminate). My class library contains a single...
7
by: Atul Malaviya | last post by:
>From a design/usability perspective. When will one use a singleton pattern and when a class with purely static members? What are the pros and cons? I have inherited a code base which is full...
1
by: Peteroid | last post by:
Why don't managed classes (i.e., _gc garbage-collected classes) support 'const', 'virtual', and 'static'? Is there a reason they can't, or is these possibly going to be supported in the future? ...
9
by: Chuck Cobb | last post by:
I am creating some static classes with static methods and static variables, but I have a question: What is the lifetime of static classes and static variables? Is there any risk that these...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.