Hi all,
do you know a good tool to find memory leaks/ memory allocation in c#?
Many thanks
Printable View
Hi all,
do you know a good tool to find memory leaks/ memory allocation in c#?
Many thanks
You do realize that C# is a managed language and you really can't leak memory.
However, the only way I know of that it might *appear* to leak memory is have some event handler with a bunch of references to what are supposed to be temporary objects.
IE:
PermanentObject.Event += new SomeEventHandler( TempObject );
but even if I use something like:
using (DataSet ds = new DataSet())
{
SqlDataAdaper da = new SqlDataAdapter();
da.Fill(ds,"TableName")
}
I see that tha application allocates a lot of memory and doesn't free it.
This is how the garbage collector works and is expected.Quote:
I see that tha application allocates a lot of memory and doesn't free it.
Put simply it keeps allocating memory until it runs out and then garbage collects to free up memory.
This is not a memory leak. In fact it's nothing to worry about : just keep coding, you'll be fine.
Darwen.
I kind of meant the pure managed part, not that other stuff, but yes, you can definitely leak memory anytime you are using unmanaged code.
The problem is that I launch several instances of this application and I have not enough RAM in the server.
I need to free memory as soon as the variable (i.e. dataset) goes out of scope.
Even if I call GC.collect() i am not ble to release it.
I believe there is a command, GC.Collect() you can run that will release any available memory.
http://msdn.microsoft.com/en-us/library/xe0c2357.aspx
Have you considered using an IDataReader instead ?Quote:
I need to free memory as soon as the variable (i.e. dataset) goes out of scope.
DataSets bulk download all the results from the server, which for queries which return a large number of results occupies a large amount of memory.
IDataReader (i.e. Command.ExecuteReader) gets one record at a time (or rather streams them) which is much more efficient with memory.
Also I'd look to the query you're running on your server. If you're doing filtering at client side try changing the filtering to being done as part of your SQL statement. This will not only reduce the number of results returned by the server (causing your memory issues) but also will cause the query to run much faster since fewer results need to be transferred between server and client.
Darwen.