[fpc-devel] GetHeapStatus.TotalAllocated

Nikolai Zhubr n-a-zhubr at yandex.ru
Sat Feb 13 23:26:28 CET 2010


13.02.2010 16:33, Jonas Maebe:
>> 2) GetHeapStatus.TotalAllocated sometimes return negative values, though I haven't been able to prepare a reasonably small example yet (should I?)
>
> Yes.

I haven't yet succeeded in reproducing negative values in a small 
example, however I think the program below demonstrates some 
misbehaviour already, because after deallocation, TotalAllocated stays 
as before deallocation. The output is:
In program: 0 M
In thread: 2 M
In thread: 2 M
Deallocated! <<<=== After this, TotalAllocated should go down somewhere?
In program: 0 M
In thread: 2 M <<<=== ... but it doesn't!
In program: 0 M
In thread: 2 M

--------------------------------------------------------
program heapstatustest;
{$mode delphi}
{$apptype console}

uses
   SysUtils, Classes;

type
   TMyThread = class(TThread)
     procedure Execute; override;
   end;

procedure ShowAllocatedSize(s: string);
begin
   writeln(s + IntToStr(GetHeapStatus.TotalAllocated shr 20) + ' M');
end;

var
   p: pointer = nil;

procedure TMyThread.Execute;
begin
   GetMem(p, 1024*1024*2);
   repeat
     ShowAllocatedSize('In thread: ');
     sleep(1000);
   until false;
end;

begin
   TMyThread.create(false);
   repeat
     if p <> nil then
     begin
       FreeMem(p, 1024*1024*2);
       p := nil;
       writeln('Deallocated!');
     end;
     ShowAllocatedSize('In program: ');
     sleep(1000);
   until false;
end.




More information about the fpc-devel mailing list