TMySQL50Connection memory leak lazarus / 16-Databases ---------------------------------------------------------------------- DimProfi · 2012-06-08 00:26 Code: [Select]procedure TForm1.Button2Click(Sender: TObject); var     conn:TMySQL50Connection;     query:TSQLQuery;     trans:TSQLTransaction;      c:integer; begin   for c:=0 to 100000 do   begin          conn:=TMySQL50Connection.Create(nil);          conn.HostName:='localhost';          conn.DatabaseName:='?database?';          conn.UserName:='root';          conn.Password:='';          trans:=TSQLTransaction.Create(nil);          trans.DataBase:=conn;          query:=TSQLQuery.Create(nil);          query.DataBase:=conn;          query.Transaction:=trans;          conn.Open;          query.SQL.clear;          query.SQL.Add('select * from ?table? LIMIT 1000');          query.ExecSQL;          FreeAndNil(query);          FreeAndNil(trans);           conn.Close;           FreeAndNil(conn);      end;    showmessage('ok!'); end; 1. Replace "?database?" and "?table?" with your some of your mysql database (I used XAMPP for Windows on localhost for this test to speed up) 2. Start Task-Manager, look at Memory-Usage for 5-10 Minutes. It grows and grows about 1 MB in 3 Minutes and it doesnt stops after 30 minutes. Thats not good for my software which runs 2-3 month and frequently connecting to the database. But the main question is: is it a bug? how can i eliminate it? I tested with Lazarus 0.9.30.4 x86_64-win64  on windows 7 64bit. ---------------------------------------------------------------------- Lacak2 · 2012-06-08 05:19 Do changes something, when you use instead of:          query.ExecSQL;          query.Open; ? ---------------------------------------------------------------------- DimProfi · 2012-06-08 05:31 Quote from: Lacak2 on June 08, 2012, 07:19:17 amDo changes something, when you use instead of:          query.ExecSQL;          query.Open; ? Ok, I made it. As expected, no changes. RAM usage grows about 1 MB in 30 second. ---------------------------------------------------------------------- ludob · 2012-06-08 05:33 Checked your code with heaptrc, the cmem memory manager and a memory monitor tool: there is no leak in the pascal code. However the memory increase (confirmed with cmem) is apparently coming from libmysql (5.0.22 on my system). See http://bugs.mysql.com/bug.php?id=45002 or http://bugs.mysql.com/bug.php?id=47841 for pascal. Edit: found that the leak is memory allocated in libmysql but not linked to above bug reports. When calling ExecSQL with a command that returns a result set, the result set is not cleared with Mysql_free_result. The suggestion made by Lacak2 (query.open, instead of query.execsql)  is working for me: closing the dataset calls Mysql_free_result, no more memory increase. FPC 2.7.1 So this is wrong usage of datasets and yes, it is also a bug in TMySQLxxConnection. I'll report the bug and try to create a patch for it. Edit: http://bugs.freepascal.org/view.php?id=22228 ---------------------------------------------------------------------- DimProfi · 2012-06-08 12:02 Thanks a lot! So i will try to minimize a leak