473,324 Members | 2,268 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,324 software developers and data experts.

Lifetime of Static Classes and Variables

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 items might be garbage collected
if they're not used for a period of time?

For example:

public static class test
{
static Collection<string> coll;

public static void AddItem()
{
coll = new Collection<string>();
coll.Add("abc");
}

public static string GetItem(int index)
{
return coll[index];
}
}

Suppose I call the AddItem method and nothing is holding a reference to this
class, what's to prevent it from being garbage collected and destroyed?

Do static members like this live for the life of the application without
being subject to garbage collection?

Thanks,

Chuck Cobb

Apr 9 '06 #1
9 20512
Is this net 2.0 version of .NET?

On my version (1.1) static can not be used to describe a class.

On my version of .NET I find static members static members live for the
duration of the application.

I can't realy imagine a circumstance where a GC could act on static
data, Ithink th GC does not go there at all and the memory is release
only on application closure.

I'd be looking to use a singleton pattern rather then have a static
collection.

-dm

Apr 10 '06 #2
Chuck Cobb <ch********@oxfordcorp.com> wrote:
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 items might be garbage collected
if they're not used for a period of time?


Static variables exist for the lifetime of the ApplicationDomain they
live in.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 10 '06 #3
You got my curiosity going,

I sniffed around and found this excelent article by Matthew Cochran.

http://www.c-sharpcorner.com/UploadF...7-e861d5aecd87

It actuly has your exact problem in it (towards the end) and shows a
little memory problem that might occur with the way you have set up
your class (not the one you were worried about though).

Infact the other sections of the article make for good reading as well.

-dm

Apr 10 '06 #4
Static methods are not objects that are subject to garbage collection, so
they have no lifetime issues for you to deal with.

For each static field there is one copy per-appdomain - each appdomain can
have a different value. The static variable itself is not garbage
collected - it is just a memory location that contains a reference to an
object. Static fields are considered to be GC roots, so any references in
them will not be garbage collected as long as the field contains that
reference. If you change the field to point to a new reference or set the
field value to null then the object whose reference was stored there becomes
eligible for collection.

Static class constructors will run once for each appdomain they get loaded
in.

Per your example, the method AddItem() is not an object that can get
garbage collected, it is a method. Each time you call AddItem a new
Collection object will be allocated and its reference assigned to the field
coll. Any previous reference stored in that field will be eligible for
garbage collection, but the current reference will not be GCd.
"Chuck Cobb" <ch********@oxfordcorp.com> wrote in message
news:OJ**************@TK2MSFTNGP05.phx.gbl...
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 items might be garbage collected
if they're not used for a period of time?

For example:

public static class test
{
static Collection<string> coll;

public static void AddItem()
{
coll = new Collection<string>();
coll.Add("abc");
}

public static string GetItem(int index)
{
return coll[index];
}
}

Suppose I call the AddItem method and nothing is holding a reference to
this class, what's to prevent it from being garbage collected and
destroyed?

Do static members like this live for the life of the application without
being subject to garbage collection?

Thanks,

Chuck Cobb

Apr 10 '06 #5
th*********@gmail.com wrote:
You got my curiosity going,

I sniffed around and found this excelent article by Matthew Cochran.

http://www.c-sharpcorner.com/UploadF...7-e861d5aecd87

It actuly has your exact problem in it (towards the end) and shows a
little memory problem that might occur with the way you have set up
your class (not the one you were worried about though).

Infact the other sections of the article make for good reading as well.


It's unfortunately inaccurate in terms of methods and memory, implying
that each object carries around its own copy of all the instance
methods for that type. I'll add a comment to that effect...

Jon

Apr 10 '06 #6
Jon Skeet [C# MVP] wrote:
It's unfortunately inaccurate in terms of methods and memory, implying
that each object carries around its own copy of all the instance
methods for that type. I'll add a comment to that effect...


Grr... I would have added a comment, but their login seems to be
totally broken :(

Jon

Apr 10 '06 #7
On 10 Apr 2006 00:41:49 -0700, th*********@gmail.com wrote:
You got my curiosity going,

I sniffed around and found this excelent article by Matthew Cochran.

http://www.c-sharpcorner.com/UploadF...7-e861d5aecd87

It actuly has your exact problem in it (towards the end) and shows a
little memory problem that might occur with the way you have set up
your class (not the one you were worried about though).

Infact the other sections of the article make for good reading as well.

-dm


The author of that article has some serious misunderstandings when
trying to explain methods. A misunderstanding that unfortunally seems
very common. It tries to put to much information about methods on the
heap.

Ordinary methods doesn't actually have any data at all on either the
stack or the heap. It is all stored in the code section. The only
exception is virtual/abstract methods that keep one V-Table on the
heap for each class.

So, how does method calling work? First it should be noted that static
methods and instance methods work exactly the same.

instance.Method(5);

is the same as

Method(instance,5);

It is only when virtual methods and inheritance that it becomes a
little more complicated. When calling a virtual method, code has to
look at the instance which contains a pointer to its class. It will
then look into the V-Table to find out which method to call.

To answer the original poster. Static variables are stored once for
each class and lives until the application ends. Static classes
doesn't have anything special compared to normal classes, except for
not having a default public constructor.

--
Marcus Andrén
Apr 10 '06 #8
Thanks! This has been a good discussion and was very helpful....

Chuck Cobb

"Chuck Cobb" <ch********@oxfordcorp.com> wrote in message
news:OJ**************@TK2MSFTNGP05.phx.gbl...
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 items might be garbage collected
if they're not used for a period of time?

For example:

public static class test
{
static Collection<string> coll;

public static void AddItem()
{
coll = new Collection<string>();
coll.Add("abc");
}

public static string GetItem(int index)
{
return coll[index];
}
}

Suppose I call the AddItem method and nothing is holding a reference to
this class, what's to prevent it from being garbage collected and
destroyed?

Do static members like this live for the life of the application without
being subject to garbage collection?

Thanks,

Chuck Cobb

Apr 10 '06 #9
Marcus,

Thanks, I handn't picked up on the articles problem (skim reader)... I
might take a look at it again later.

Anyway I stilll don't like the idea of static collections... I personly
would use a singleton for what its worth. if nothing else it allows the
colection to be stored properly if ths class to be serialized.
-dm
-dm

Apr 11 '06 #10

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

Similar topics

1
by: Torsten Mueller | last post by:
I have to create a shared library containing C++ classes with static member variables on HPUX with aCC. I can compile and link the library and a test application but when I start the program I get...
2
by: joes | last post by:
Hi folks! I noticed in my static example which I did in PHP that the static variable is not stored over multiple php pages. So this does differ than to other OO languages like JSP. Is this so or...
3
by: Colin Desmond | last post by:
I have an MFC Extension DLL that I have flicked the /clr switch on and made a few other tweaks so it compiles and runs in a managed MFC applicaiton. The DLL contains a number of classes which...
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
4
by: thomson | last post by:
Hi All, Can any one tell me the demerits of using static classes in Web Application,what is the scope of this kind of class Thanks in Advance Thomson
4
by: JC | last post by:
Suppose an ASP.Net project contains a public static class with public methods and members that are used throughout the application. Of course being static, there is only copy of the class within...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
4
by: Rene | last post by:
Hi, I was wondering if anyone could tell me why extension methods must be declared on static classes. I mean, why not allowing them to be declared as static methods in regular instance classes?...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.