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?

Comments

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

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

    ReplyDelete
  2. Also it's probably not a terribly useful figure anyway

    ReplyDelete
  3. David Heffernan It has helped me track down leaks on many occasions. It's not accurate, but it is a good indicator.

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

    ReplyDelete
  5. I can share some code if you are interested for Linux. It is actually pretty difficult to do accurately on Posix platforms.

    ReplyDelete
  6. Allen Drennan That could come in handy!

    ReplyDelete
  7. Lars Fosdal I looked again and everything I do uses native OS functions, not the memory manager directly, sorry.

    ReplyDelete
  8. 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.

    ReplyDelete
  9. 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

    ReplyDelete

Post a Comment