CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2002
    Posts
    146

    tool for memory leak

    Hi all,
    do you know a good tool to find memory leaks/ memory allocation in c#?

    Many thanks

  2. #2
    Join Date
    Sep 2004
    Posts
    1,361

    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 );

  3. #3
    Join Date
    Jul 2006
    Posts
    297

    Re: tool for memory leak

    Quote Originally Posted by DeepT View Post
    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.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: tool for memory leak

    Quote Originally Posted by DeepT View Post
    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. )

  5. #5
    Join Date
    Jul 2002
    Posts
    146

    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.

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    Sep 2004
    Posts
    1,361

    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.

  8. #8
    Join Date
    Jul 2002
    Posts
    146

    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.

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    Re: tool for memory leak

    Quote Originally Posted by DeepT View Post
    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.

  10. #10
    Join Date
    Jul 2006
    Posts
    297

    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

  11. #11
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured