Bug #84791 Memory leak in Connector/Net
Submitted: 2 Feb 2017 9:25 Modified: 30 Mar 2017 7:44
Reporter: Antonis Pa Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:6.9.9 OS:Windows
Assigned to: CPU Architecture:Any
Tags: memory leak

[2 Feb 2017 9:25] Antonis Pa
Description:
I have a database with a table that contains 1,200,000 rows and about 30 columns. I use MySql.Data and MySql.Data.Entity  to read data from this table. 
More specific I have use 10 tasks to read the entire table. I can see that my memory increases to 3GB+ which us insanely crazy without dropping even though when the reading finishes.

How to repeat:
This is my code

 public void MultiThreading()
        {
            try
            {
                IEnumerable<IEnumerable<int>> splittedLists = ...

                Task[] listOfTasks = new Task[10];
                foreach (var listOfUsers in splittedLists)
                {
                    Task tsk = new Task(() => GetProducts(listOfProducts.ToList()));
                    listOfTasks[count] = tsk;
                    tsk.Start();
                }

                Task.WaitAll(listOfTasks);

            }
            catch (Exception ex)
            {

            }
        }

        public void GetProducts(List<int> listOfProducts)
        {
            using (MySqlConnection connection = new MySqlConnection(connectionString))
            {
                try
                {
                    using (MT4DbContext context = new MT4DbContext(connection, false))
                    {
                        connection.Open();

                        foreach (var product in listOfProducts)
                        {
                            var products = (from prod in context.Products
                                              where prod.Id == product.Id
                                              select prod).ToList();
                        }
                    }

                }
                catch (Exception ex)
                {

                }
            }
        }
[2 Feb 2017 9:48] Antonis Pa
As per my mem profiler I can see that these objects get a lot of memory

Type, Objects count, Bytes, Minimum retained bytes
System.Data.Entity.Core.Objects.EntityEntry, 695673, 72349992, 1291162080
System.Data.Entity.Core.Objects.Internal.EntityWrapperWithoutRelationships<Products>, 695673, 61219224, 172526904
System.Data.Entity.Core.EntityKey, 695675, 50088600, 66784980
System.Data.Entity.Core.Objects.DataClasses.RelationshipManager, 695673, 44523072, 77915376
System.Collections.Generic.List<StateManagerValue>, 695672, 27826880, 1046286536
[2 Feb 2017 9:58] Antonis Pa
The largest object type is this

Nodes from "All objects in the snapshot"

Type, Objects count, Bytes, Minimum retained bytes
System.Data.Entity.Core.Objects.StateManagerValue[], 695675, 550970760, 1018460496
[2 Feb 2017 10:06] Antonis Pa
This is a screenshot of my mem profiler

Attachment: memleak.PNG (image/png, text), 15.80 KiB.

[2 Feb 2017 10:06] Antonis Pa
This is a screenshot of my mem profiler

Attachment: memleak.PNG (image/png, text), 15.80 KiB.

[20 Feb 2017 7:33] Chiranjeevi Battula
Hello  Antonis,

Thank you for the bug report.
I could not repeat the issue at our end using with Visual Studio 2013, Connector/NET 6.9.9 with .net core and EF Core.
Could you please provide repeatable test case (exact steps/sample project, screenshot etc. - please make it as private if you prefer) to confirm this issue at our end?

Thanks,
Chiranjeevi.
[20 Feb 2017 7:38] Antonis Pa
Unfortunately I cannot provide sample as this is part of a huge enterprise project.

I am using VS2015, I am using

<packages>
  <package id="EntityFramework" version="6.0.0" targetFramework="net46" />
  <package id="MySql.Data" version="6.9.9" targetFramework="net46" />
  <package id="MySql.Data.Entity" version="6.9.9" targetFramework="net46" />
</packages>

Try to run the read process with the code that I attached in my first comment with multiple threads.

Thanks
[30 Mar 2017 7:44] Chiranjeevi Battula
Hello  Antonis,

Thank you for the bug report.
I could not repeat the issue at our end using with Visual Studio 2015, Connector/NET 6.9.9.
If you can provide more information, feel free to add it to this bug and change the status back to 'Open'.

Thank you for your interest in MySQL.

Thanks,
Chiranjeevi.