在使用TIdTcpServer运行的应用程序中,如何防止TFDQuery导致过多的内存消耗?
我在运行时创建TFDQuery,使用后我在TIdTcpServer的OnExecute事件上销毁它:
Query := TFDQuery.Create(Cn);
Query.Connection := Cn;
Query.SQL.Text := 'update table set column = 0 where ip = :ip';
Query.Params.ParamByName('ip').Value := ip;
Query.ExecSQL;
FreeAndNil(Query);
每个新的连接都会在MSSQL上执行选择/插入/更新,因此我总是创建/销毁对象,但是内存仍在增加(我正在与在TcpServer上创建各种连接的客户端进行测试)
我已经测试过,并且如果我从OnExecute应用程序内存中删除了TFDQuery,那么在测试中总是可以的。
cn
是TFDConnection,它们始终处于活动状态,并在应用程序启动时创建并在应用程序关闭时销毁。
答案 0 :(得分:0)
在运行时使用这种创建/销毁方法解决了
with TFDQuery.Create(nil) do
begin
try
Connection := Cn;
SQL.Text := 'update table set column = 0 where ip = :ip';
Params.ParamByName('ip').Value := ip;
ExecSQL;
finally
Free;
end;
end;