Possible Memory Leak in MySQL connection
Posted: Fri 25 Jan 2008 11:57
Hi,
We have memory problems in a multithreaded application. We create a number of threads to read a number of large blobs (4-20MB each) from the database. The memory rises sharply at the beginning but never goes down to the orignal level.
A memory profiler shows that the memory is in a byte array in the MySQlInternalConnection. Even after closing and disposing the connection the memory remains the same. It looks as if the connection is keeping a reference to the data that was read out last.
We are using version 3.5, I have downloaded the latest trial version 4.30 as well and get the same results. Developing using Visual Studio 2005 .
I have attached code below, I can also send a test database when required.
can you let me know if there is something wrog
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CoreLab.MySql;
using System.Threading;
using System.Collections;
using System.Diagnostics;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static MySqlConnection GetConnection()
{
string connectionString;
connectionString = "server = localhost;";
connectionString += "database = Test DB;";
connectionString += "user id = root;";
connectionString += "pwd=;";
MySqlConnection connection = null;
try
{
connection = new MySqlConnection( connectionString );
}
catch
{
connection.Dispose();
connection = null;
return null;
}
try
{
connection.Open();
}
catch
{
if( connection != null )
{
connection.Dispose();
connection = null;
}
return null;
}
return connection;
}
private long GetMemory()
{
long page = Process.GetCurrentProcess().PagedMemorySize64 / 1024;
long ram = Process.GetCurrentProcess().WorkingSet64 / 1024;
long total = page + ram;
return total;
}
private void button1_Click( object sender, EventArgs e )
{
ArrayList threads = new ArrayList();
Thread thread;
long startmem = GetMemory();
for( int i = 0; i 0 )
{
using( MySqlBlob mySqlBlob = reader.GetMySqlBlob( 0 ) )
{
if( mySqlBlob != null && mySqlBlob.Value != null )
mySqlBlob.Dispose();
}
}
}
}
catch( Exception ex )
{
Console.WriteLine("Error:" + ex.Message);
}
}
connection.Close();
connection.Dispose();
connection = null;
GC.Collect();
}
}
}
We have memory problems in a multithreaded application. We create a number of threads to read a number of large blobs (4-20MB each) from the database. The memory rises sharply at the beginning but never goes down to the orignal level.
A memory profiler shows that the memory is in a byte array in the MySQlInternalConnection. Even after closing and disposing the connection the memory remains the same. It looks as if the connection is keeping a reference to the data that was read out last.
We are using version 3.5, I have downloaded the latest trial version 4.30 as well and get the same results. Developing using Visual Studio 2005 .
I have attached code below, I can also send a test database when required.
can you let me know if there is something wrog
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CoreLab.MySql;
using System.Threading;
using System.Collections;
using System.Diagnostics;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static MySqlConnection GetConnection()
{
string connectionString;
connectionString = "server = localhost;";
connectionString += "database = Test DB;";
connectionString += "user id = root;";
connectionString += "pwd=;";
MySqlConnection connection = null;
try
{
connection = new MySqlConnection( connectionString );
}
catch
{
connection.Dispose();
connection = null;
return null;
}
try
{
connection.Open();
}
catch
{
if( connection != null )
{
connection.Dispose();
connection = null;
}
return null;
}
return connection;
}
private long GetMemory()
{
long page = Process.GetCurrentProcess().PagedMemorySize64 / 1024;
long ram = Process.GetCurrentProcess().WorkingSet64 / 1024;
long total = page + ram;
return total;
}
private void button1_Click( object sender, EventArgs e )
{
ArrayList threads = new ArrayList();
Thread thread;
long startmem = GetMemory();
for( int i = 0; i 0 )
{
using( MySqlBlob mySqlBlob = reader.GetMySqlBlob( 0 ) )
{
if( mySqlBlob != null && mySqlBlob.Value != null )
mySqlBlob.Dispose();
}
}
}
}
catch( Exception ex )
{
Console.WriteLine("Error:" + ex.Message);
}
}
connection.Close();
connection.Dispose();
connection = null;
GC.Collect();
}
}
}