delphi XE6 TIdHTTP.Get (Indy 10) 内存消耗

delphi XE6 TIdHTTP.Get (Indy 10) memory consuption

我有这个功能

function doHTTPRequest(const AUrl: String): String;
// ------------------------------------------------------------------------
// return the response for a http request
var
   aIdHTTP : TIdHTTP;
begin
   Result  := '';

   aIdHTTP := TIdHTTP.Create(nil);
   try
     aIdHTTP.HandleRedirects := true;
     aIdHTTP.RedirectMaximum := 5;
     aIdHTTP.ConnectTimeout  := 5000;
     aIdHTTP.ReadTimeout     := 10000;

     Result := aIdHTTP.Get(aUrl);

     aIdHTTP.Disconnect(false); 

     if Assigned(aIdHTTP.IOHandler) then
        aIdHTTP.IOHandler.InputBuffer.Clear;

   finally
      if Assigned(aIdHTTP) then FreeAndNil(aIdHTTP);
   end;
end;

每次调用此函数时,"task manager" 中的进程都会增加 200 字节(或多或少)的私有工作集内存。

我哪里错了?

我已经使用了 FastMM 和 AQTime,但没有发现内存泄漏

这是我在任务管理器中看到的示例,从 beginend 内存增加了 200 字节并且从未释放...

doHTTPRequest 
    begin = 3.480 KB
    end   = 3.652 KB
    ----------------
    diff.     184 Byte

doHTTPRequest return 值为 html 字符串(约 20 KB)

doHTTPRequestTIdHttpServer 调用,如下所示:

procedure TServer.CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
   AResponseInfo.ContentText := doHTTPRequest('http://example.com/foo.html');
end

首先,如果您只是想在之后立即释放 TIdHTTP 对象,则无需调用 Disconnect()InputBuffer.Clear()。如果套接字仍处于连接状态,析构函数将为您关闭并释放套接字。

其次,TIdHTTP.Get() 确实在下载内容时在内部分配内存,但它也会释放它分配的所有内存。在您的示例中,Get() 返回已下载内容的最终 String 表示。您必须记住 Delphi 的内存管理器(System 单元中的 FastMM by default) DOES NOT return freed memory back to the OS, the memory is cached for later re-use. That is what Task Manager is showing you. Task Manager only knows about the memory that your process has allocated from the OS, but it does not know how your process is actually using that memory. You cannot use Task Manager to diagnose memory leaks. You have to use FastMM's own leak tracking features instead, since only it knows which memory is actually leaked or not. You can enable the global ReportMemoryLeaksOnShutdown 变量,但 FastMM 的默认安装已启用最小泄漏报告。有关更详细的报告,请安装将 FastMM 的完整版本添加到您的项目中并根据需要进行配置。