This piece of code is Windows only:

This piece of code is Windows only:

function MemoryUsed: cardinal; inline;

var

MMS: TMemoryManagerState;

Block: TSmallBlockTypeState;

begin

GetMemoryManagerState(MMS);

Result := MMS.TotalAllocatedMediumBlockSize + MMS.TotalAllocatedLargeBlockSize;

for Block in MMS.SmallBlockTypeStates

do Result := Result + (Block.UseableBlockSize * Block.AllocatedBlockCount);

end;

Is there something that is available on other platforms (POSIX) that can give me an idea of how much memory the app is using, without calling native OS functions?

13 thoughts on “This piece of code is Windows only:”

  1. I thought GetMemoryManagerState it about Delphi / FastMM4 not Windows ?

    Won’t it work on any platform where you compiled FastMM4 ?

  2. I notice that I have been blocked from viewing David Heffernan’s most excellent contributions to these forums. Truly, a great loss indeed.

  3. Allen Drennan well, I guess that would add a bit of overhead, since I call the function every time I log something, but it could be worth trying.

  4. Lars Fosdal Could it be you are asking the wrong question? On windows calling other processes is very slow and inefficient so you might want to call a windows API like the above. On linux, and especially with a function you clearly aren’t going to be calling every microsecond the os functions make it so much easier to do things like you’ve done above. For example, this code allows you to run a shell command and get the output

    function commandLineTaskWithOutput( cmdLine: string ): TStringStream;

    var

    Handle: TStreamHandle;

    Data: array[0..511] of uint8;

    m1,m2: TMarshaller;

    i: integer;

    begin

    result := TStringStream.create(”,TEncoding.UTF8);

    try

    Handle := popen(M1.AsUTF8(cmdLine).ToPointer,’r’);

    try

    if integer(Handle) 0 then

    begin

    for i := 0 to length(data)-1 do

    data[i] := 0;

    while fgets(@data[0],Sizeof(Data),Handle)nil do begin

    result.writeString( Utf8ToString(@Data[0]) );

    for i := 0 to length(data)-1 do

    data[i] := 0;

    end;

    end;

    finally

    if integer(Handle) 0 then

    pclose(Handle);

    end;

    except

    on E: Exception do

    iSystemLog.LogMessage(E.ClassName + ‘: ‘ + E.Message);

    end;

    end;

    commandLineTaskWithOutput(‘cat /proc/meminfo’)

    Pass the output, read MemTotal/MemFree

Leave a Reply