[C#]How to release resources used by objects?
Yes... me again...
Observe this project (Multiupload.com - upload your files to multiple file hosting sites!).
It does simple task (for now, and probably will do so in future).
Basically it is like a simple web server. Listens for connections, once there is connection, read header, do shit, send data back.
No matter if it's C#, C, C++ or any other language I could never figure out how to release resources used by objects. THo I didn't care much.
But now I do and I tried what I could think of, such as using dispose method on some objects, and setting objects to null.
Doesn't help much I guess.
Would love to read some advices from ragezone members on how to properly dispose all or at least most resources taken by objects.
This projects is used as a server-side for ajax requests. Client makes a request to this server and this server sends response with data.
Re: [C#]How to objects to free resources?
Well I couldn't really tell you an effective way using a managed language, but I can tell you effective ways using C++
Putting data you create dynamically into containers, then during the deconstruction method you delete all the data within the container.
another way is creating and deleting "on-the-fly" meaning you create and destroy as you will. C# and other managed languages usually give you a generational GC, and usually you don't need to to force your objects to delete themselves and what not. Though it is good practice, and can be a hard habit to break especially if you are coming from a language that is unmanaged, or has no built-in memory management.
Re: [C#]How to objects to free resources?
Quote:
Originally Posted by
cmb
Well I couldn't really tell you an effective way using a managed language, but I can tell you effective ways using C++
Putting data you create dynamically into containers, then during the deconstruction method you delete all the data within the container.
another way is creating and deleting "on-the-fly" meaning you create and destroy as you will. C# and other managed languages usually give you a generational GC, and usually you don't need to to force your objects to delete themselves and what not. Though it is good practice, and can be a hard habit to break especially if you are coming from a language that is unmanaged, or has no built-in memory management.
So since C# is managed language, it already has some sort of garbage collector? I did small test where I simply started to make a lot of requests and see how much memory server eats. It jumped quiet high but then once requests stopped the memory usage dropped down. 20 minutes after the memory usage was around 10-20% more than on start up.
I will rewrite code into a service and let it run on server and forget about it. But dont wanna end up with a mess after a month or so.
Re: [C#]How to release resources used by objects?
You can't just look at memory usage for any .NET app. It won't make any sense.
Memory is generally only released once there's "pressure" on the system. The garbage collector often doesn't bother to run if there's no pressure. It might say it's using 100MB in task manager, but as soon as something else on the machine needs it, it'll clean up and release a bunch of memory :P
Anyway, nearly all things in C# don't need to be cleaned up. Once you're done with'em just ignore'em.
Exceptions are things like Sockets and Files. Technically you don't need to clean those up either. The garbage collector will get around to it. But if you want to immediately clean up stuff like that, just call Dispose on them.
Re: [C#]How to release resources used by objects?
Quote:
Originally Posted by
onemorejosh
You can't just look at memory usage for any .NET app. It won't make any sense.
Memory is generally only released once there's "pressure" on the system. The garbage collector often doesn't bother to run if there's no pressure. It might say it's using 100MB in task manager, but as soon as something else on the machine needs it, it'll clean up and release a bunch of memory :P
Anyway, nearly all things in C# don't need to be cleaned up. Once you're done with'em just ignore'em.
Exceptions are things like Sockets and Files. Technically you don't need to clean those up either. The garbage collector will get around to it. But if you want to immediately clean up stuff like that, just call Dispose on them.
How about my own classes? I assume I would have to make my own Dispose method with this = null;
Right?
Re: [C#]How to release resources used by objects?
Most classes you ever make will never need a Dispose() method
If you want to release a resource like a FileStream or a Socket at a particular time, then yeah you could implement Dispose() and clean up there...
But for a class like:
Code:
class meow
{
byte[] littlebuffer = new byte[65536];
}
class blarg
{
int x, y;
byte[] somehugebuffer = new byte[99999999];
meow m = new meow();
}
You dont need a Dispose() method because there's nothing that won't be automatically recovered when it's done being used