I have this function
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;
Every time I call this function, the process in "task manager" increase private working set memory of 200 Byte (more or less).
where am I wrong?
I already use FastMM and AQTime but no memory leak have been found
this is an example of what i can see in task manager, from the begin
to end
the memory is increased by 200 Byte and it is never released...
doHTTPRequest
begin = 3.480 KB
end = 3.652 KB
----------------
diff. 184 Byte
doHTTPRequest
return value is html string (about 20 KB)
doHTTPRequest
is called by TIdHttpServer
like this:
procedure TServer.CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
AResponseInfo.ContentText := doHTTPRequest('http://example.com/foo.html');
end
First, there is no need to call Disconnect()
or InputBuffer.Clear()
if you are just going to free the TIdHTTP
object immediately afterwards. The destructor will close and free the socket for you if it is still connected.
Second, TIdHTTP.Get()
does allocate memory internally when downloading content, but it also frees any memory it allocates. In your example, Get()
is returning a final String
representation of whatever content was downloaded. You have to keep in mind that Delphi's memory manager (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
variable in the System
unit, but the default installation of FastMM has minimal leak reporting enabled. For more detailed reporting, install the full version of FastMM into your project and configure it as needed.