Back

PRF020

Memory stream in loop

Object Pascal — Delphi & Free Pascal
Warning
Performance

Creating TMemoryStream per iteration wastes allocations when one stream can be reused

creating TMemoryStream per iteration wastes allocations when one stream can be reused.

Why this severity

creating TMemoryStream per iteration wastes allocations when one stream can be reused.


Known false positives

iterations where each stream is handed off to an owning consumer.

Remediation example

Stream := TMemoryStream.Create;
try
  for I := 0 to Count - 1 do
  begin
    Stream.Clear;
    LoadInto(Stream, Items[I]);
  end;
finally
  Stream.Free;
end;

Reconnecting… Connection lost. Reload