Avoiding memory leaks
-
Being a bit of a beginner with .Net and c#, I have a questino on cleaning up resources. In Delphi we would free and object using Object.Free and in the scheme of things all objects had virtual method that would be overridden to clear items within that object. For examlpe i might have an object that contains a bunch of array lists, or memory streams. I have hear of garbage collection and that C# free's up memory usage automatically, but i'm not sure how true that is and how deep it goes. In C# if I have MyObject class with some arraylists populated with other objects that are created by MyObject and i use this object in some button click for example, when the click is done will MyObject and all its other objects get released? Do I need some special routine that the system will automatically call when it goes to dispose of MyObject? Hope that makes sense...
-
Being a bit of a beginner with .Net and c#, I have a questino on cleaning up resources. In Delphi we would free and object using Object.Free and in the scheme of things all objects had virtual method that would be overridden to clear items within that object. For examlpe i might have an object that contains a bunch of array lists, or memory streams. I have hear of garbage collection and that C# free's up memory usage automatically, but i'm not sure how true that is and how deep it goes. In C# if I have MyObject class with some arraylists populated with other objects that are created by MyObject and i use this object in some button click for example, when the click is done will MyObject and all its other objects get released? Do I need some special routine that the system will automatically call when it goes to dispose of MyObject? Hope that makes sense...
dwolver wrote:
i'm not sure how true that is and how deep it goes.
It is very true and it is very deep (but only for managed objects)
dwolver wrote:
i use this object in some button click for example, when the click is done will MyObject and all its other objects get released?
Yes, eventually. The garbage collector does not run as soon as something is no longer referenced anywhere, it runs when it needs to. And then it will clean up the objects.
dwolver wrote:
Do I need some special routine that the system will automatically call when it goes to dispose of MyObject?
Now, you've hit on the word I was going to mention next, "Dispose". If an object uses unmanaged resources then you must implement the
IDisposable
interface. This requires you to implement a method calledDispose()
that is used to clean up the objects that the garbage collector cannot. So, if you use unmanaged objects or another class that implements theIDisposable
interface (like a file stream) then you are responsible for cleaning up the object. You can callDispose()
manually, but a much neater solution is something like this:using (FileStream fs = new FileStream(someFileName))
{
// Do something with the FileStream
}What happens here is that you tell the compiler that when you exit the
using
block to callDispose
on the object you created at the start. It doesn't matter how you exit the block. You can callreturn
to exit the method as a whole, an exception can be thrown, whatever. The compiler will ensure that the object'sDispose
method is called on your behalf.* Developer Day Scotland 2 - Free community conference * The Blog of Colin Angus Mackay
Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.
-
Being a bit of a beginner with .Net and c#, I have a questino on cleaning up resources. In Delphi we would free and object using Object.Free and in the scheme of things all objects had virtual method that would be overridden to clear items within that object. For examlpe i might have an object that contains a bunch of array lists, or memory streams. I have hear of garbage collection and that C# free's up memory usage automatically, but i'm not sure how true that is and how deep it goes. In C# if I have MyObject class with some arraylists populated with other objects that are created by MyObject and i use this object in some button click for example, when the click is done will MyObject and all its other objects get released? Do I need some special routine that the system will automatically call when it goes to dispose of MyObject? Hope that makes sense...
THe fundamentals of .NET are that it manages your memory for you. If an object does not have a Dispose method to call ( and if it does, you should call it ), then you shouldn't have to worry about memory. That's what garbage collection means.
Christian Graus Driven to the arms of OSX by Vista.
-
Being a bit of a beginner with .Net and c#, I have a questino on cleaning up resources. In Delphi we would free and object using Object.Free and in the scheme of things all objects had virtual method that would be overridden to clear items within that object. For examlpe i might have an object that contains a bunch of array lists, or memory streams. I have hear of garbage collection and that C# free's up memory usage automatically, but i'm not sure how true that is and how deep it goes. In C# if I have MyObject class with some arraylists populated with other objects that are created by MyObject and i use this object in some button click for example, when the click is done will MyObject and all its other objects get released? Do I need some special routine that the system will automatically call when it goes to dispose of MyObject? Hope that makes sense...
hi.......... Donn't worry abt memory leaks.. U know one of the important featuere of c#.NET is Its Effective Garbage Collection feature..... If u want to know more , just click below link.... About Garbage Collection
-
dwolver wrote:
i'm not sure how true that is and how deep it goes.
It is very true and it is very deep (but only for managed objects)
dwolver wrote:
i use this object in some button click for example, when the click is done will MyObject and all its other objects get released?
Yes, eventually. The garbage collector does not run as soon as something is no longer referenced anywhere, it runs when it needs to. And then it will clean up the objects.
dwolver wrote:
Do I need some special routine that the system will automatically call when it goes to dispose of MyObject?
Now, you've hit on the word I was going to mention next, "Dispose". If an object uses unmanaged resources then you must implement the
IDisposable
interface. This requires you to implement a method calledDispose()
that is used to clean up the objects that the garbage collector cannot. So, if you use unmanaged objects or another class that implements theIDisposable
interface (like a file stream) then you are responsible for cleaning up the object. You can callDispose()
manually, but a much neater solution is something like this:using (FileStream fs = new FileStream(someFileName))
{
// Do something with the FileStream
}What happens here is that you tell the compiler that when you exit the
using
block to callDispose
on the object you created at the start. It doesn't matter how you exit the block. You can callreturn
to exit the method as a whole, an exception can be thrown, whatever. The compiler will ensure that the object'sDispose
method is called on your behalf.* Developer Day Scotland 2 - Free community conference * The Blog of Colin Angus Mackay
Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.
Excellent answer. I'd just stress that the key sentence is: So, if you use unmanaged objects or another class that implements the IDisposable interface (like a file stream) then you are responsible for cleaning up the object. Too often seen replies that say in .Net you can completely disregard memory management issues because of automatic GC. Regards David R