I tried to run a SELECT SQL to retrieve some data from MySql database. For this, I had created a TZQuery inside a TThreadn to avoid freezing the GUI of the application.
But, after the end of each consult (tested with 100,000+ rows, just to "force" a freeze) the memory was increasing instead of returning to the initial values (even after FreeAndNil(ZQuery) and FreeAndNil(TThread)) increasing ~2mb to each query executed (verified using Windows taskman.)
The test:
1 - Click button1 to start thread and query
2 - Wait callback
3 - Click button2 to finish and free query
{ TForm }
procedure TForm1.Button1Click(Sender: TObject);
begin
//DBQuery: TSubThreadTest;
DBQuery:= TSubThreadTest.Create(SQLStatement, true, ZCon, @onQueryCallBack);
DBQuery.Start;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FreeAndNil(DBQuery);
end;
procedure TForm1.onQueryCallBack(fail: boolean; query: TZQuery);
begin
lLastExec.Caption:= FormatDateTime('hh:mm:ss', Now);
end;
{ TSubThreadTest }
constructor TSubThreadTest.Create(SQLStatement: string; toExec: boolean;
connector: TZConnection; EOnConclude: TSubThreadTestCallBack);
begin
inherited Create(true);
//main: TZQuery;
main:= TZQuery.Create(nil);
main.Connection:= connector;
main.SQL.Text:= SQLStatement;
isToExec:= toExec;
OnConclude:= EOnConclude;
FreeOnTerminate:= False;
end;
procedure TSubThreadTest.callback;
begin
if assigned(OnConclude) then
OnConclude(true, main);
end;
procedure TSubThreadTest.Execute;
begin
if isToExec then
main.ExecSQL
else
main.Open;
Synchronize(@callback);
FreeAndNil(main);
end;
First thread create and call: 24mb memory usage
Second thread create and call: 26,5mb memory usage
Etc...