I'm wondering if it's possible to do it without garbage collection.
If I have a variable (reference type), can I "delete" it from memory anyhow?
I think C++ had a delete operator, uh cant remember:(
Printable View
I'm wondering if it's possible to do it without garbage collection.
If I have a variable (reference type), can I "delete" it from memory anyhow?
I think C++ had a delete operator, uh cant remember:(
I would think that the GC.Collect method should be adequate in most situations. You may also like to read this. It may also interest you to know that .NET 2.0 has a SecureString class, instances of which are removed (somehow) from memory once they are no longer referenced. Good for passwords and the like.
Correct me if I am wrong here. But in C++ you don't delete a reference either. Because a reference is just a reference, and not the actual object. So if you have a reference in C++, you know that it is NOT your job to delete it. It is the guy that sits on the actual object, or the pointer to the object (here it get difficult when you have many pointers too it) that has the actual job to delete it.
But in C#, you are supposed to let the GC to collect it when it is ready to get deleted. The destructor doesn't work the same way in C++ as in C#. In C++ it deltetes the whole object, and voila. But in C#, it deletes the what you have spesified in the destructor, but not the object it self. The object it self doesn't get delted before the GC is ready to run again. And it doesn't run all the time. But as a final word on this. To make sure your resources gets deleted more or less when you want, you have to implement the IDisposable interface, and then explicitly call the Dispose() method.
- ØØ -
It is bad practice to call GC.Collect, it is a, comparitively, very slow procedure.
The logic behind GC is that it will be called automatically when it is needed by the operating system, the need not the call, usually very infrequently.
I'm sure some geek here will be able to cite an scenario when GC.Collect should be called but I can't think of one.
Basically; create an object, use it, forget it.
I am no geek, but after loading a world in a game, and you are ready to play the level can be a good place to call it..:)
But as a general rule, yes, it is not something you are supposed to do during run time.
- ØØ -
APPARENTLY it's not called automaticallyQuote:
Originally Posted by GlenW
As an example: I was working on an app where many many instances of images are loaded and then disposed. I was calling the Dispose method of each item properly but the ram usage would reach 2GB :) and then the operating system would puke:D
I "had to" call GC.Collect in my case so I dunno
that's why I wanted to know if there's a way to get rid of somethign manually, so I wouldn't have to call GC.Collect
My thoughts (feel free to discard them)
* It is good practice to use GC.Collect because you are telling the app that this is a good time to dump a load of ram. However, this must not be overused, and certainly NEVER inside a loop. You should notify users that a long ram dump is occuring.
* If you are using 2gb's of ram then its time for a serious rethink of your duty cycle.