Groups | Blog | Home
all groups > dotnet faqs > january 2004 >

dotnet faqs : Garbage Collectionin .NET


Ed Kaim [MSFT]
1/18/2004 10:10:34 PM
That isn't necessary. As long as your object is referenced, it won't be
garbage collected. After all, if your object isn't referenced somehow, then
you wouldn't be able to access it anyway. The article at
http://msdn.microsoft.com/msdnmag/issues/1200/gci2/default.aspx gives an
overview of garbage collection in the .NET Framework.

[quoted text, click to view]

Randy A. Ynchausti
1/18/2004 10:39:32 PM
Rimonne,

[quoted text, click to view]

All you have to do is make sure your application has a reference to the
object. Therefore, it will never be released.

Regards,

Randy

Glen Jones MCSD
1/19/2004 6:54:44 AM
Hello,

I suggest creating a "C# static" variable to keep the referance.

Hope this helps.

--
Glen Jones MCSD

[quoted text, click to view]

Mattias Sjögren
1/19/2004 7:05:41 AM

[quoted text, click to view]

No, GC.SuppressFInalize just prevents the Finalize method from being
called (good for performance) once the object is collected.

As Randy said, keep a live reference to the object.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Morten Wennevik
1/19/2004 7:24:35 AM
..NET keeps track of your objects through a set of reference tables.

Every now and then (you have no control of when, though you can attempt =
to =

force it using System.GC.Collect()) the garbage collector will go throug=
h =

these tables and flag objects your program has no references to anymore.=

It will then unravel these objects (which may have references of its own=
) =

and release it from memory.
After that it will rearrange your objects to tidy up the use of memory =

(allocating memory under .NET is actually faster than in native C).

However, since you have no control of when it runs, time and performance=
=

critical applications may find that the garbage collector may run at a =

time you really don't want it to. In that case, you can turn it off =

alltogether and handle all memory by yourself.

Another limitation is using resources, like file handlers and database =

connections, of which it isn't very good at cleaning up itself. So any =

object that has a Close() method you should call Close() on when you are=
=

finished using it.

PS! To release an object simply set the reference to null. The object =

will float into the unknown, waiting to be eaten by the garbage collecto=
r.

-- =

Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
For a laugh, try web browsing with Opera's User Mode and Nostalgia enabl=
Morten Wennevik
1/19/2004 7:27:48 AM
On Sun, 18 Jan 2004 22:10:34 -0800, Ed Kaim [MSFT] =

[quoted text, click to view]
=

[quoted text, click to view]

Actually, you can. You can write code in the object that refreshes it's=
=

reference when it is about to be collected. I can't think of any reason=
=

to do that, but you can.

-- =

Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
For a laugh, try web browsing with Opera's User Mode and Nostalgia enabl=
Rimonne
1/19/2004 10:09:28 AM
Hi All

I understand that the .net framework has garbagecollectors which run
automatically and release memory.

But in my application in C#, i have an object which should never be
released.

Can I achieve this in .NET ?

An urgent response is neede, as I am struck up with my development work
here.

Thanks a lot for your time.

Regards.

Jay B. Harlow [MVP - Outlook]
1/19/2004 10:26:29 AM
Rimonne,
Have you considered implementing the Singleton Pattern? (or a variation
there of).

http://www.yoda.arachsys.com/csharp/singleton.html

Hope this helps
Jay

[quoted text, click to view]

Milica Bogovac
1/19/2004 10:52:29 AM
[quoted text, click to view]


I think that:

Object.Dispose();

should do it.

Regards, Milica

Rimonne
1/19/2004 11:12:56 AM
Cant i use
GC.SuppressFinalize method for this? Just read it in msdn.

[quoted text, click to view]

Daniel O'Connell
1/19/2004 1:07:52 PM
This article applies to remoted objects, not to local objects. A local
object does not require activity to remain in existance.
[quoted text, click to view]

Manoj G [MVP]
1/19/2004 5:34:04 PM
Oops! That is something we are trying not to do. If Dispose is called on
the object, you are infact telling that the resources held by object be
released (and hence no finalization be required).
As some folks already said, in order to achieve what you trying, I guess you
can keep the reference of the object somewhere alive. This can be a static
member of class and you make sure that you do not set the this to
null/nothing in your code. This object's gonna be there till the appdomain
in which this object exists unloads.

--
HTH,
Manoj G [.NET MVP]
Site: http://www15.brinkster.com/manoj4dotnet
Blog: http://msmvps.com/manoj/

[quoted text, click to view]

Max-Ph. Blickenstorfer
1/19/2004 6:09:30 PM
Hi Rimone,
After reading the various replies you've got so far. My reply would be best
described in this article:
http://msdn.microsoft.com/msdnmag/issues/03/12/leasemanager/default.aspx

Acording to above article it is not enought to hold a reference to the
object, the object must recognize some activity. To make sure the connection
will not be marked for garbage collection you have to work with "Leases".
For each created object the framework creates a lease. This lease is managed
by the frameoworks lease manager. By default an object is marked as inactive
after 5 minutes inactivity. However, you can manipulate the lease behaving
and create a callback interface on your clients side. This callback function
will be called by the lease manager and your client can specify whether the
server side object can be disposed or not. In fact it would be good your
client side object implements IDispose, so your client can release resources
when you feel it's appropriate for it.

Another good article covering remoting objects is in CodeMagazine Jan/Feb
2003. Even nearli a year old - it contains quite a bit content.
http://www.code-magazine.com/

Anyway, I hope the above linkes can bring some light into this behaving.

Regards
Max




[quoted text, click to view]

Max-Ph. Blickenstorfer
1/20/2004 7:30:13 AM
Hi Daniel,
You're right - my head currently sticks in remoting objects to .net and mfc
clients and of course lifetime/validity of those objects.

Thanks for the justification.

Regards
Max

[quoted text, click to view]

AddThis Social Bookmark Button