Bug #80727 Bug: MySqlCommand did not release resources and potential cause memory exhausted
Submitted: 14 Mar 2016 13:18 Modified: 22 Mar 2016 14:29
Reporter: jun wang Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.9 OS:Microsoft Windows (Windows 7)
Assigned to: CPU Architecture:Any

[14 Mar 2016 13:18] jun wang
Description:
The case summary:
In while loop to execute insert sql statement, and use the memory profile to analyze the code, it found that there are instances(System.Transations.SafeIUnknown) always increased. This resource can’t be released and it is shown that System.Transation.SafeIUnkown is created by MySqlCommand (Method: ExecuteReader).

Detail Information:
Code:           
  for (int i = 0; i < strSqlCmdLst.Count; i++)
  {
    log.Debug("sql statement:" + strSqlCmdLst[i]);
    cmd.CommandText = strSqlCmdLst[i];
    cmd.ExecuteNonQuery();
  }

Expectation: The cmd is instance of MySqlCommand. It is expected that no MySqlCommand instance increased and also no MySql related instance increased in memory profile mornitor. 

Problem: It find System.Transactions.SafeIUnknown increased.

How to repeat:
Run the code 
  for (int i = 0; i < strSqlCmdLst.Count; i++)
  {
    log.Debug("sql statement:" + strSqlCmdLst[i]);
    cmd.CommandText = strSqlCmdLst[i];
    cmd.ExecuteNonQuery();
  }

Suggested fix:
It may invoke COM object or Ptr which is not released.
[14 Mar 2016 13:20] jun wang
Memory Profile screen 1

Attachment: Bug20160314_01.png (image/png, text), 68.35 KiB.

[14 Mar 2016 13:20] jun wang
Memory Profile screen 2

Attachment: Bug20160314_02.png (image/png, text), 78.22 KiB.

[15 Mar 2016 7:43] Chiranjeevi Battula
Hello jun wang,

Thank you for the bug report.
I tried to reproduce the issue at my end using Visual Studio 2013 (C#.Net) and Connector/Net 6.9.8 but not seeing any issues in memory.
Could you please provide repeatable test case (create table statements, list of commands etc. - please make it as private if you prefer) to confirm this issue at our end?

Thanks,
Chiranjeevi.
[15 Mar 2016 7:44] Chiranjeevi Battula
Screenshot.

Attachment: 80727.PNG (image/png, text), 114.17 KiB.

[21 Mar 2016 13:16] jun wang
Hello Chiranjeevi Battula

     Sorry for late resonse. I add project file in attachment.
     There are two projects and one sql script.
     1. CreateTable_Sql.txt: create the table for test
     2. TestMemory: the project which show the mysql object(MySqlCommmand, MySqlConnection) work normal. In memory profiler, it show mysql object stable.
     3. TestMem: the project which show the mysql object((MySqlCommmand, MySqlConnection) work abnormal. In memory profiler, it show mysql object increased and not released.
[21 Mar 2016 13:16] jun wang
test project

Attachment: TestMemory.rar (application/octet-stream, text), 30.71 KiB.

[21 Mar 2016 13:25] jun wang
When run test memory project, it show mysql object normal

Attachment: Bug20160321_01_TestMemory.png (image/png, text), 150.46 KiB.

[21 Mar 2016 13:26] jun wang
when run test mem project, it show mysql object abnormal and instance increased continually

Attachment: Bug20160321_02_TestMem.png (image/png, text), 137.61 KiB.

[21 Mar 2016 13:28] jun wang
Hello Chiranjeevi Battula 

    My running environment is Microsoft Visual Studio 2012(C#.Net), windows 7

Thanks.
[22 Mar 2016 6:39] Chiranjeevi Battula
Hello jun wang,

Thank you for your feedback and test case.
Verified this behavior on Visual Studio 2013 (C#.Net) with  MySQL Connector/Net 6.9.8.

Thanks,
Chiranjeevi.
[22 Mar 2016 6:40] Chiranjeevi Battula
Screenshot.

Attachment: 80727_1.png (image/png, text), 175.97 KiB.

[22 Mar 2016 6:40] Chiranjeevi Battula
Screenshot.

Attachment: 80727_2.png (image/png, text), 158.95 KiB.

[22 Mar 2016 14:29] jun wang
Hello Chiranjeevi Battula

     I found the difference for project TestMem with TestMemory. The project out type both are Windows Application. In TestMem, there is no invocation of below code which I commented.

           //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            
            // if comment below line, it will show mysql command instance can't be released and continually increased
            //Application.Run(new Form1());

     And if I change the project TestMem out type to Console Application and also change code in Program.cs as below, it work normally and show mysql object normally.

    /*
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            */
    class Program
    {
        static void Main(string[] args)
        {

Thanks.