473,396 Members | 1,853 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,396 software developers and data experts.

Garbage Collectionin .NET

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.
Nov 15 '05 #1
14 1158

While your object be usefull will be sure. the garbage collector release an object when you can't use it any more

I hope this help
Nov 15 '05 #2
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
Nov 15 '05 #3
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

Nov 15 '05 #4
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.
Nov 15 '05 #5
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


Nov 15 '05 #6
..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
Nov 15 '05 #7
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
Nov 15 '05 #8
"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
Nov 15 '05 #9
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

Nov 15 '05 #10
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.

Nov 15 '05 #11
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.

Nov 15 '05 #12
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.

Nov 15 '05 #13
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.


Nov 15 '05 #14
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.



Nov 15 '05 #15

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

Similar topics

13
by: Rimonne | last post by:
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. ...
22
by: Rimonne | last post by:
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. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.