Debugging, Profiling and Memory Leak Detection
47.1 IDE Debugger
F5. Conditional, pass count, log message, group actions. Data breakpoints (address watches).
F7 into, F8 over, Shift+F8 run until return, F4 run to cursor Ctrl+F5. Evaluate/Modify (Ctrl+F7)
Local Variables, Call Stack, Threads, Modules, Event Log View → Debug Windows Disassembly, registers, memory Inspect objects in depth (Alt+F5)
PAServer (mobile/macOS/Linux), remote debugger for Windows Tools → Options → Debugger: notify on exceptions, ignore specific types
Custom display for TDateTime, TStringList, etc.
{$IFDEF DEBUG}
OutputDebugString(PChar('Value = ' + IntToStr(X))); // view in the Event Log or DebugView
{$ENDIF}
Assert(Count >= 0, 'Count must not be negative'); // {$C+}47.2 Logging and Crash Reporting
CodeSite (bundled Express edition), LoggerPro (open source), Log4D, System.Logging, the Windows Event Log, file logs with rotation.
Crash reporting with stack traces: madExcept, EurekaLog, JclDebug (requires map/debug info: Project Options → Linking → Map file Detailed).
47.3 Memory Leaks and Profiling
ReportMemoryLeaksOnShutdown := True, FastMM4 FullDebugMode / FastMM5 (leak stack traces, use-after-free detection), DeLeaker, madExcept leak checking.
Profilers: Nexus Quality Suite, AQTime (legacy), ProDelphi, Sampling Profiler, Intel VTune/Windows Performance Toolkit with map2pdb symbols, TStopwatch for micro-benchmarks, Spring4D benchmarking helpers.
47.4 Performance Tips
Use const parameters for strings/records/interfaces. Avoid unnecessary string concatenation (use TStringBuilder or string.Join). Preallocate (SetLength, Capacity). Use TDictionary for lookups.
Avoid excessive with, Variant use, and RTTI in hot paths. Enable optimization and disable range/overflow checks in release builds only after testing.
VCL: BeginUpdate/EndUpdate on lists and grids, DisableControls/EnableControls on datasets, double buffering, virtual list views (OwnerData), LockWindowUpdate cautiously.
Database: parameterized prepared queries, FetchOptions (RowsetSize, Mode), array DML, indexes, avoiding RecordCount on large remote datasets.
Use 64-bit for large memory. Use the PPL for CPU-bound work. Consider FastMM5 for multi-threaded allocation performance.