-2

I have a C# WinForm Application that updates and select from a MySQL database too frequently (about 500 request per minute). It is a simple application that listens to Websocket events and update the MySql db accordingly, I tried testing the application with MSSQL and it was fine.

The problem is that the memory usage keeps adding-up, I tried disabling MySQL Pooling, and all the connections and commands are being used with using (as i know, this should dispose the connections and commands) but still having the same problem.

Do you have any suggestions for me?

        cs="server=localhost;userid=root;password=password;database=dbmcf;"
        try
        {
            using (var con = new MySqlConnection(cs))
            {
                con.Open();
                var sql = "select id,login,is_active from user";
                using (var cmd = new MySqlCommand(sql, con))
                {

                    using (MySqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            UserModel model = new UserModel();
                            model.Id = rdr.GetInt32(0);
                            model.Login = rdr.GetString(1);
                            model.Active = rdr.GetBoolean(2);
                            lstUser.Add(model);
                            
                        }
                        rdr.Close();
                        cmd?.Dispose();
                    }
                }
                con.Close();

            }

         }
        catch { }
        }
doubler
  • 7
  • 2
  • 1
    keep track of changes and dump changes after 5-6 min. – Akash Kansara Aug 24 '23 at 10:37
  • @AkashKansara the data should be reflected to the database immediately the max i can wait is 3 seconds – doubler Aug 24 '23 at 10:41
  • can you add a snippet of code on how you connect and dispose? – Akash Kansara Aug 24 '23 at 10:42
  • @AkashKansara added! – doubler Aug 24 '23 at 10:51
  • Does https://stackoverflow.com/questions/1178736/mysql-maximum-memory-usage help? – Jay Buckman Aug 24 '23 at 10:59
  • @JayBuckman no the mysql server is not leaking memory, my C# application is – doubler Aug 24 '23 at 11:05
  • How are you determining memory usage? – Jay Buckman Aug 24 '23 at 11:21
  • Every time you run this code, you're adding to `lstUser` - that's basically going to take memory. As an aside, an empty catch block is *almost always* unhelpful. If something goes wrong, you have no way of telling what... – Jon Skeet Aug 24 '23 at 11:43
  • @JonSkeet the lstUser is being cleared on a separate method, and as i said if the lstUser was the problem, then why it is working fine with MSSQL? – doubler Aug 24 '23 at 12:08
  • I missed the part about MSSQL before - but you should clarify the list aspect in your question and I'd strongly recommend trying the code without the try/catch. (You shouldn't need any of the Close or Dispose calls, by the way - given that you're already using `using` statements. In modern C# you can avoid all the nesting, too...) – Jon Skeet Aug 24 '23 at 12:56
  • Use the Memory Usage Tool in Visual Studio (https://learn.microsoft.com/en-us/visualstudio/profiling/memory-usage) or dotMemory (https://www.jetbrains.com/dotmemory/) to determine what's causing the excess memory usage. – Bradley Grainger Aug 24 '23 at 12:59

0 Answers0