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

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.