首页 > 解决方案 > 关于使用 TIdTcpServer 的 FDQuery

问题描述

如何防止 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。

标签: delphiindyfiredac

解决方案


在运行时使用这种创建/销毁方法解决:

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;

推荐阅读