Warfley, GetFPCHeapStatus.CurrHeapUsed is correct for detecting leaks (e.g. in tests), it has nothing to do with reusing etc.
No it's not correct for detecting memory leaks, and I have shown you an examply why it is not:
uses
heaptrc;
var
i: Integer;
p: PInteger;
begin
heaptrc.keepreleased := True; // Override reusing behavior
Write(GetFPCHeapStatus.CurrHeapUsed,' bytes => ');
for i := 0 to 10000 do
begin
new(p);
dispose(p);
end;
WriteLn(GetFPCHeapStatus.CurrHeapUsed);
end.
This code is completely memory leak free. The heap trace output states:
10001 memory blocks allocated : 40004/80008
10001 memory blocks freed : 40004/80008
0 unfreed memory blocks : 0
True heap size : 2326528 (160 used in System startup)
True free heap : 86144
Should be : 2326368
There is no unfreed memory blocks. This code does not leak any memory
Yet the output from CurrentHeapUsed is:
CurrentHeapUsed is not a metric that is useful for detecting memory leaks, because the memory manager may decide to keep freed memory around for some reason or another. Your way of measuring memory leaks relies on certain assumptions on how the memory manager works. But as the code above shows, these assumptions are not always true.
So instead of using a metric that may work when your assumptions may line up with reality, what you should do instead is use a metric that is designed to do the work you want to do. Again valgrind and heaptrc are specifically built to verify if you have memory leaks or not, while as you can see in the example above, CurrentHeapUsed is not.