1

Problem: The memory leaks & accumulates over time, and eventually reaches its 99% capacity

This is a question I previously asked on the same topic. I did as the guy who answered told me to do, but I am still experiencing memory leak issues. I really do not understand from where the memory is accumulating. I observed the Windows Task Manager, and found out that periodically memory clears, but the memory accumulation rate is faster than clearing rate, and as a result, memory capacity reaches 99% at the end.

Here is my C# code:

var connString = "Host=x.x.x.x;Port=5432;Username=postgres;Password=password;Database=database";
@Info.Trace("PostGre ");

using (var conn = new Npgsql.NpgsqlConnection(connString)){

    conn.Open();

    int ctr = 0;

    // Insert some data
    using (var cmd = new Npgsql.NpgsqlCommand())
    {
        cmd.Connection = conn;

        var par_1 = cmd.Parameters.Add("@r", NpgsqlTypes.NpgsqlDbType.Timestamp);
        var par_2 = cmd.Parameters.Add("@p", NpgsqlTypes.NpgsqlDbType.Double);

        while(@tag.TerminateTimeScaleLoop == 100)
        {   
            @Info.Trace("Pushed Data: PostGre A " + ctr.ToString());

            cmd.CommandText = "INSERT INTO TORQX VALUES (@r,@p)";
            par_1.Value = System.DateTime.Now.ToUniversalTime();
            par_2.Value = @Tag.RigData.Time.TORQX;
            cmd.ExecuteNonQuery();            

            ctr = ctr + 1;
        }


    }
    @Info.Trace("Pushed Data: PostGre A Terminated");
    conn.Close();              

}

What is causing the memory accumulation? Can I prevent it from accumulating? If preventing accumulation is impossible, can I manually clear memory? What set of codes will do that?

I have practically no experience with C#, and I was assigned to do a hot fix on this C# code because the person who wrote this code isn't available now. I have lots of experience in Python, but no experience with C#, so please give me suggestions in a really explicit way... otherwise I will have no clue. Thanks!

2
  • 1
    What do the traces do? Nothing in this piece of code should cause any persistent memory usage otherwise. Commented Aug 19, 2018 at 5:06
  • Unused memory is wasted memory. The CLR will recycle and reuse available ram as the GC heap grows. It going up to 99% is not a sign something is going wrong, especially when working with sql databases of any decent size. Commented Aug 19, 2018 at 5:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.