|
-
August 24th, 2009, 03:42 PM
#1
tool for memory leak
Hi all,
do you know a good tool to find memory leaks/ memory allocation in c#?
Many thanks
-
August 24th, 2009, 04:23 PM
#2
Re: tool for memory leak
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 );
-
August 24th, 2009, 04:31 PM
#3
Re: tool for memory leak
 Originally Posted by DeepT
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 );
C# is managed MOST of the time. You can still leak memory using unmanaged code.
-
August 24th, 2009, 04:40 PM
#4
Re: tool for memory leak
 Originally Posted by DeepT
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 );
You can definitely leak memory in .NET, though it is more rare than in unmanaged languages. You can maintain 'stale' references as you say, or you can fail to release unmanaged resources (i.e., not calling Dispose() when a class implements IDsposable. )
-
August 24th, 2009, 04:45 PM
#5
Re: tool for memory leak
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.
-
August 24th, 2009, 04:49 PM
#6
Re: tool for memory leak
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.
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.
-
August 24th, 2009, 04:51 PM
#7
Re: tool for memory leak
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.
-
August 24th, 2009, 04:58 PM
#8
Re: tool for memory leak
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.
-
August 24th, 2009, 06:47 PM
#9
Re: tool for memory leak
 Originally Posted by DeepT
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.
Code:
void Leak( )
{
Form s = new Form( );
s.Show( );
}
There is a possible memory leak, no unmanaged code required.
-
August 24th, 2009, 11:56 PM
#10
Re: tool for memory leak
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
-
August 25th, 2009, 03:43 AM
#11
Re: tool for memory leak
I need to free memory as soon as the variable (i.e. dataset) goes out of scope.
Have you considered using an IDataReader instead ?
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|