Search code examples
delphiindyidhttp

"Out of memory while expanding memory stream" error when attempting to download a file using TIdHTTP


I am attempting to download a file from a web server using the standard TIdHTTP and TIdSSLIOHandler components in Delphi 10.4:

memorystream := TMemoryStream.Create;
http.request.useragent:='Mozilla/5.0 (Windows NT 5.1; rv:2.0b8) Gecko/20100101 Firefox/4.0b8';
http.HandleRedirects := True;
try
  http.get('https://dl.nmkd.de/ai/clip/coco/coco.ckpt',memorystream);
except
  on E:Exception do memo.lines.add(E.Message);
  memo.lines.add(http.responsetext);
end;
application.ProcessMessages;
memorystream.SaveToFile('coco.ckpt');
memorystream.free;

The responses I get in the memo are:

Out of memory while expanding memory stream
HTTP/1.1 200 OK

The file is around 9 GB in size.

How do I get a MemoryStream to be able to handle that size? I have more than enough physical RAM installed in the PC.


Solution

  • Instead of loading the resource to memory temporarily, directly load it to the local file using a TFileStream:

    Stream := TFileStream.Create('coco.cpkt');
    try
      try
        http.get('https://dl.nmkd.de/ai/clip/coco/coco.ckpt', Stream);
      except
        on E:Exception do memo.lines.add(E.Message);
      end;
    finally
      Stream.Free;
    end;