Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-08

TFPCHeapStatus miscalculations in 2.3.1

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Seth Grover

I haven't logged a bug because I haven't yet figured out the steps to exactly reproduce this, but I've been seeing something strange since I've started using 2.3.1.

I have a thread pool in my application which gets assigned to run a "job" by the user, does so, and then waits until it's called on again to run another job. Since 2.3.1 gives a different heap to each thread, I've added debugging code to periodically log the heap status returned by GetFPCHeapStatus. At first, everything starts out fine:

0xB0A93BB0 heap status: cu=1456, cs=786432, cf=784976, mu=30720, ms=1048576 0xB0691BB0 heap status: cu=1456, cs=786432, cf=784976, mu=14080, ms=1048576 0xAFE85BB0 heap status: cu=1456, cs=786432, cf=784976, mu=30720, ms=1048576

Notice that "current size" minus "current free" equals "current used", just like you'd expect.

After running for a few hours though, look at my numbers:

0xB0287BB0 heap status: cu=4294952816, cs=786432, cf=800912, mu=4294967280, ms=5144576
0xAFA83BB0 heap status: cu=4294873136, cs=1146880, cf=1241040, mu=4294967280, ms=5603328
0xB22C1BB0 heap status: cu=4294950256, cs=950272, cf=967312, mu=4294967280, ms=5570560
0xB26CABB0 heap status: cu=4294956896, cs=983040, cf=993440, mu=4294967280, ms=5570560
0xAF681BB0 heap status: cu=4294885088, cs=917504, cf=999712, mu=4294967280, ms=5603328
0xAF27FBB0 heap status: cu=4294844176, cs=1179648, cf=1302768, mu=4294967280, ms=5603328
0xB1EADBB0 heap status: cu=4294963824, cs=950272, cf=953744, mu=4294967280, ms=5570560
0xB3EE6BB0 heap status: cu=4294956896, cs=950272, cf=960672, mu=4294967280, ms=5570560

One by one, over time, the "current free" becomes *greater* than the "current size", so the calculations are all screwy (current used is negative, etc.).

Does anyone know about changes to this which could cause this problem? It's rather annoying... I know the each heap isn't really using 4.2 gigabytes, but I wish the numbers were accurate.

-SG

This email is fiction. Any resemblance to actual events or persons living or dead is purely coincidental. Seth Grover sethdgrover[at]gmail[dot]com
Seth Grover
to Seth Grover

Actually I was able to distill it down into a smaller example:

------------------------------------------------
program Project1;
{$mode objfpc}{$H+}
uses
  Classes, SysUtils;
function RandomRange(const low : longint;
                     const high : longint) : longint;
begin
  if (high < low) then
    result := high + random(low - high + 1)
  else
    Result := low + random(high - low + 1);
end;
procedure GetStats;
var
  fpcHeapStatus : TFPCHeapStatus;
begin
  fpcHeapStatus := GetFPCHeapStatus();
  writeln(' heap status: cu=' +
          IntToStr(fpcHeapStatus.CurrHeapUsed) + ', cs=' +
          IntToStr(fpcHeapStatus.CurrHeapSize) + ', cf=' +
          IntToStr(fpcHeapStatus.CurrHeapFree) + ', mu=' +
          IntToStr(fpcHeapStatus.MaxHeapUsed)  + ', ms=' +
          IntToStr(fpcHeapStatus.MaxHeapSize));
end;
var
  i : integer;
  a : array of byte;
begin
  randomize();
  for i := 0 to 1000 do begin
    SetLength(a, RandomRange(1024,1024*1024*15));
  end;
  GetStats();
  SetLength(a, 0);
  GetStats();
end.
------------------------------------------------

My output:

$ ./project1
heap status: cu=4277286960, cs=16154624, cf=33834960, mu=4294897312, ms=31752192
heap status: cu=4276332896, cs=458752, cf=19093152, mu=4294897312, ms=31752192

Seems like that should never happen. I'll log an issue in the bug tracking system.

-SG

This email is fiction. Any resemblance to actual events or persons living or dead is purely coincidental. Seth Grover sethdgrover[at]gmail[dot]com
Vincent Snijders
to Seth Grover
Seth Grover wrote
Actually I was able to distill it down into a smaller example: ------------------------------------------------ randomize();
For reproducebility, can you change it to
   RandSeed := (some *constant* you choose);

Vincent