Page 1 of 1

TScHttpWebRequest memory leak

Posted: Wed 16 Feb 2022 09:43
by AliakseiY
Hi,
I am trying to requst a web interface with a http post command.
Everything is going well and I get a response (xml string) from the interface .
But while executing this task up to 1.3 Gb RAM is being used.
If the request has been done, up to 600 Mb of memory ist still being used by the programm.

Can you support me with this issue?
Thanks.

It is my delphi code (RAD Studio 10.1 Berlin):

procedure Tfrmmain.btnrequestClick(Sender: TObject);
var
Response: TScHttpWebResponse;
ResponseStr: string;
buff: TBytes;
xml: String;
igspn: TScHttpWebRequest;
begin
try
xml:= ' ### original xml string ###';

igspn:= TScHttpWebRequest.Create(Application);
igspn.RequestUri:= '### original URL ###';
igspn.ContentType:= 'text/xml';
igspn.KeepAlive:= true;
igspn.Method:= rmPOST;
igspn.headers.Add('Authorization','Basic '+ TBase64Encoding.Base64.Encode('##username##+ ':' + '##password##'));
buff:= TEncoding.UTF8.GetBytes(xml);
igspn.ContentLength:= Length(buff);
igspn.WriteBuffer(buff);
igspn.SendChunked := false;
Response:= igspn.GetResponse;
memo1.Lines.Text:= Response.ReadAsString;
FreeAndNil(response);
except on e: Exception do begin
ShowMessage(e.Message);
end;
end;
end;

Re: TScHttpWebRequest memory leak

Posted: Mon 21 Feb 2022 12:18
by YanishevskiyVI
Hi!

Please note that when using the ReadAsString method in the way you described, the memory is allocated three times: once in the ReadAsString method itself, once as output result and one more time in Memo itself.

To solve the issue, in order to reduce the consumption of RAM, you can use the TScHttpWebResponse.ReadToStream method by specifying an object of the TFileStream class in the parameters, in which case the server response will be saved to disk and not to RAM.

Re: TScHttpWebRequest memory leak

Posted: Tue 02 Aug 2022 07:59
by ualms
Is this Problem solved?
I need to send a lot requests. If i call httpwebrequest.GetResponse the used memory go up. The Call httpwebresponse.free do not release the memory.
This is a problem, because i have to call up to 10000 requests.

Re: TScHttpWebRequest memory leak

Posted: Fri 05 Aug 2022 13:24
by Dimon
Check if a memory leak occurs if you release the HttpWebRequest object, not just the HttpWebResponse.