Free Pascal 也有类似的功能。在程序结束时,调用DumpHeap
或启用 Lazarus 项目设置中的 heaptrc 选项。可以使用SetHeapTraceOutput
方法设置输出文件。两种方法都在heaptrc
必须是项目中第一个的单元中(从一开始就捕获分配)。
更多信息:
泄漏可视化:Lazarus 包“LeakView”在树视图中显示堆跟踪输出文件的内容。它包含在默认安装中,并在 IDE 重建后可用。(尚未经过我测试)
// By default information is written to standard output,
// this function allows you to redirect the information to a file
SetHeapTraceOutput('heaptrace.log');
// normally the heap dump will be written automatically at the end,
// but can also be written on demand any time
DumpHeap;
输出如下所示:
C:\path\to\Demo.exe
Heap dump by heaptrc unit
244 memory blocks allocated : 8305/9080
241 memory blocks freed : 8237/9000
3 unfreed memory blocks : 68
True heap size : 458752
True free heap : 458288
Should be : 458480
Call trace for block $0010CE58 size 28
$0044ACCB TIDTHREADSAFE__CREATE, line 226 of C:/path/to/indy-10.5.8.tiburon/Lib/Core/IdThreadSafe.pas
$00444245 IDTHREAD_init, line 641 of C:/path/to/indy-10.5.8.tiburon/Lib/Core/IdThread.pas
$00409D74
$0040E1A1
...
(使用 Free Pascal 2.6.0 测试)