Back

PRF004

Object creation in loop

Object Pascal — Delphi & Free Pascal
Warning
Performance

Creating and destroying objects per iteration wastes allocations and increases GC pressure

creating and destroying objects per iteration wastes allocations and increases GC pressure.

Why this severity

creating and destroying objects per iteration wastes allocations and increases GC pressure.


Known false positives

factory patterns where each iteration intentionally produces a new instance.

Remediation example

Formatter := TFormatter.Create;
try
  for I := 0 to Count - 1 do
    Formatter.Format(Items[I]);
finally
  Formatter.Free;
end;

Reconnecting… Connection lost. Reload