-2

I have a problem of "Memory Leak" in ASP.Net. I created a blank page made a connection with the database. The memory increases at a time when the page is accessed and not back to normal in no time! the highest peak memory was between 1GB !, then I have to recycle the "pool" in IIS ... I use the methods Connection.Close () and connection.Dispose (), but it seems that they do work. I searched the web more soulção could not find.

Is there any way to solve this problem without having to recycle the pool? Thanks for sharing knowledge !!!

This is the block in which the memory leak occurs:

    SqlConnection cn = new SqlConnection("");
    SqlCommand cm = new SqlCommand();

    SqlDataReader dr = null;

    try
    {
        cn.Open();
        cm.Connection = cn;
        cm.CommandTimeout = 600;
        cm.CommandText = "";

        cm.Parameters.Add("operation", SqlDbType.Int).Value = 1;
        cm.Parameters.Add("now", SqlDbType.DateTime).Value = DateTime.Now;

        dr = cm.ExecuteReader(CommandBehavior.SingleResult);
        while (dr.Read())
        {
            //blah blah blah...
        }
        dr.Close();
    }
    catch (Exception ex)
    {

    }
    finally
    {
        if (dr != null)
            dr.Dispose();

        cm.Dispose();

        cn.Close();
        cn.Dispose();
    }
CharithJ
  • 46,289
  • 20
  • 116
  • 131
Muller
  • 27
  • 1
  • 5
  • 2
    It would be a lot easier to use if you wrapped your cn/cm/dr in `using` statements, it does all the fancy stuff like closing/disposing after exceptions for you. Also, why are you using a `while` loop if you are telling it to return a single result? change the `while` to an `if`... Not that these are your problems but it cleans up your code. – Ron Beyer Jul 02 '15 at 19:04
  • my guess is your blah,blah,blah throws an exception and the finally block also crashes when it tries to dispose the unclosed dr. leaving all the objects open and un garbage collected – Ewan Jul 02 '15 at 19:19
  • Study .NET memory model first and you won't be such surprised. – Lex Li Jul 03 '15 at 03:06
  • @Ewan Things get garbage collected regardless of you calling `Dispose` or not. That is not the purpose of `Dispose`. – Cory Nelson Jul 06 '15 at 04:34
  • I hope that catch block isn't empty in your real code. – H H Jul 06 '15 at 21:20
  • Your question does not even make it clear that you have a problem. _"the highest peak memory was between 1GB !"_ is unclear and tells us nothing. When you don't have actual failures, there is no problem. – H H Jul 06 '15 at 21:22

1 Answers1

0

You are disposing all the disposable object which is good. It would be better to use 'using' instead of finally in this case. Cannot see any cause for a memory leak by looking at the code you have provided.

using (SqlConnection cn = new SqlConnection(""))
using (SqlCommand cm = new SqlCommand())
{
    .....

     using (var dr = cm.ExecuteReader(CommandBehavior.SingleResult))
     {
        while (dr.Read())
        {
            //blah blah blah...
        }
     }
}

Keep in mind that Garbage Collection doesn't release memory immediately after any instance dispose.

It has been optimized to trigger and release memory only when there is a memory stress. So, if you want to test for memory leaks you should execute Garbage Collection manually before you take memory usage readings.

GC.Collect();
GC.WaitForPendingFinalizers();
CharithJ
  • 46,289
  • 20
  • 116
  • 131