Question on static objects.

P

pitdog

Hello,

I am unable to find information on about this issue so I thought I
would post and ask.

I have a class that is using the sigleton pattern as it has an internal
static instance of itself. My question is where is that static instance
stored in .NET? Once my object is destroyed that static instance
remains in memory somewhere and I am wondering where? Is there special
space for all static objects and variables? If so when are objects put
into that space? etc...

I guess that is just part of the question what I am really looking for
is how all these things are managed in .net however I am not really
sure what to search on for this or where to look. Any insight would be
great.

Thanks,
Brette
 
S

Scott Allen

Hi Brette:

The little bit of magic behind statics is that the garbage collector
knows about static fields thanks to .NET's rich metadata. The GC will
treat static references as 'roots' that are alive and not garbage
collect objects referenced by a static field. The objects can live on
one of the heaps with all of the other objects.

Also remember that static fields are not associated with an instance
of a class, but when you say 'my object is destroyed that static
instance remains in memory' it sounds as if you still associate static
fields with the object - they are associated with the type (the
class).

Make sense?
 
P

pitdog

Yeah it does make sense. I understand what you are saying. And I think
I am clear.

I am not associating static fields with the object per say. However
this begs the question what happenes to static fields when they are
created created if they are not assoiciated directly to the object that
contains them. Am I to understand that there are stored in seperate
heap space then then object itself?

Take for example this sample class of a non thread safe sigleton =-)
Now this composite static object is created on the first call to
getSQLParser that will in turn create the compsite static object. Is
this composite object then put on the heap with all others?

Public class foo{

private static SQLParser sqlParser;

public static SQLParser getSQLParser(String inURL)
{
if(sqlParser == null)
{
sqlParser = new SQLParser(inURL);
}
return sqlParser;
}
}

Thanks alot your last post it really helped.

Brette
 
W

William F. Robertson, Jr.

I hope someone else will correct me if I an incorrect on something.

As part of the overhead of a managed object instance is a pointer to a
MethodTable. The method table is loaded once per type, so 50 instances of
class foo will all point to the same location. The MethodTable is
considered a root as far as garbage collection is concerned.

Inside this MethodTable are all the static types and declarations. So in
your example, in the MethodTable of your class foo, is a field called
SqlParser. SqlParser, when allocated, sits in the same managed heap as all
other objects created. (Unless it is a large object then sits in the Large
Object Heap). When the garbage collection runs, it walks the pointers from
the MethodTable finding a reference the SqlParser sitting in the regular old
heap, marks it as visited and leaves it alone.

MethodTables are never destroyed except when an AppDomain is unloaded, so
there is no way to "collect" a static variable without setting it to null,
and releasing the reference.

On a side note, since there is one MethodTable for every type loaded, if you
defined 7 bagillion of your own types and use them all, your MethodTable
"Heap" (I can't remember what it is called) will get very large, and since
it never purges itself...well you can picture the memory usage growing....

HTH,

bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top