By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,903 Members | 2,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,903 IT Pros & Developers. It's quick & easy.

Garbage Collectionin .NET

P: n/a
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.
Jul 21 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Rimonne,
But in my application in C#, i have an object which should never be
released.

Can I achieve this in .NET ?


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

Regards,

Randy
Jul 21 '05 #2

P: n/a
Cant i use
GC.SuppressFinalize method for this? Just read it in msdn.

"Randy A. Ynchausti" <ra*************@msn.com> wrote in message
news:Ob**************@TK2MSFTNGP11.phx.gbl...
Rimonne,
But in my application in C#, i have an object which should never be
released.

Can I achieve this in .NET ?


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

Regards,

Randy

Jul 21 '05 #3

P: n/a
Cant i use
GC.SuppressFinalize method for this? Just read it in msdn.


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
Please reply only to the newsgroup.
Jul 21 '05 #4

P: n/a
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/is...2/default.aspx gives an
overview of garbage collection in the .NET Framework.

"Rimonne" <no*@moreply.com> wrote in message
news:eF**************@TK2MSFTNGP10.phx.gbl...
Cant i use
GC.SuppressFinalize method for this? Just read it in msdn.

"Randy A. Ynchausti" <ra*************@msn.com> wrote in message
news:Ob**************@TK2MSFTNGP11.phx.gbl...
Rimonne,
But in my application in C#, i have an object which should never be
released.

Can I achieve this in .NET ?


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

Regards,

Randy


Jul 21 '05 #5

P: n/a
..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 through
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 collector.

--
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 enabled
Jul 21 '05 #6

P: n/a
On Sun, 18 Jan 2004 22:10:34 -0800, Ed Kaim [MSFT]
<ed****@online.microsoft.com> wrote:
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.


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 enabled
Jul 21 '05 #7

P: n/a
"Rimonne" <no*@moreply.com> wrote in message

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 ?

I think that:

Object.Dispose();

should do it.

Regards, Milica
Jul 21 '05 #8

P: n/a
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/

"Milica Bogovac" <mi****@datawarehouse.com> wrote in message
news:bu**********@news.eunet.yu...
"Rimonne" <no*@moreply.com> wrote in message

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 ?

I think that:

Object.Dispose();

should do it.

Regards, Milica

Jul 21 '05 #9

P: n/a
Hello,

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

Hope this helps.

--
Glen Jones MCSD

"Rimonne" <no*@moreply.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
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.

Jul 21 '05 #10

P: n/a
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

"Rimonne" <no*@moreply.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
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.

Jul 21 '05 #11

P: n/a
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/is...r/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


"Rimonne" <no*@moreply.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
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.

Jul 21 '05 #12

P: n/a
This article applies to remoted objects, not to local objects. A local
object does not require activity to remain in existance.
"Max-Ph. Blickenstorfer" <ma*@mbs-software.com> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
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/is...r/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


"Rimonne" <no*@moreply.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
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.


Jul 21 '05 #13

P: n/a
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

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:#A**************@TK2MSFTNGP11.phx.gbl...
This article applies to remoted objects, not to local objects. A local
object does not require activity to remain in existance.
"Max-Ph. Blickenstorfer" <ma*@mbs-software.com> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
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/is...r/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


"Rimonne" <no*@moreply.com> wrote in message
news:OH**************@TK2MSFTNGP12.phx.gbl...
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.



Jul 21 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.