如何防止TFDQuery在应用程序运行TIdTcpServer时导致过多的内存占用?
我在运行时创建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,它始终处于活动状态,在应用程序启动时创建,在应用程序关闭时销毁。
在运行时使用这种创建/销毁方法解决:
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;