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

Question on static objects.

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

Nov 19 '05 #1
5 1563
static objects are stored in normal memory but have pointers on the root

Nov 19 '05 #2
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?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 18 May 2005 06:48:15 -0700, pi****@gmail.com wrote:
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


Nov 19 '05 #3
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

Nov 19 '05 #4
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
<pi****@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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

Nov 19 '05 #5
Great post thanks.

I think I am clear on this all now.

Nov 19 '05 #6

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

Similar topics

15
by: Wolfram Humann | last post by:
Hi, please don't be too harsh if I made stupid errors creating this simple example from my more complex case. Suppose I have a class like this: class BOOK { const string title;
3
by: Amadelle | last post by:
Hi All and thanks in advance, I wanted to know when is a good idea to use a static class (with static constructor) and when to use instance classes? I have read couple of articles on line and...
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
15
by: dn | last post by:
I'm starting an n-tier application with an ASP.NET 2.0 presentation layer, a business layer, a data access layer, and a SQL Server 2005 database, and I have a question. In the business and data...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
8
by: maneeshkhare | last post by:
I have a doubt regarding the architecture, and working of the ASP.NET framework. I haven't been able to satisfy myself with any answer. I do understand that for each request for a resource...
12
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
13
by: John Kraft | last post by:
Friends, I'm working on some crud stuff, and I was looking for opinions on the subject. Below, I have pasted some VERY simple sample code. Class2 is a "traditional" crud type object. In a...
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: 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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.