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?
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?
/sub
ReplyDelete/sub
ReplyDeleteI thought GetMemoryManagerState it about Delphi / FastMM4 not Windows ?
ReplyDeleteWon't it work on any platform where you compiled FastMM4 ?
/sub
ReplyDeleteAlso it's probably not a terribly useful figure anyway
ReplyDeleteDavid Heffernan It has helped me track down leaks on many occasions. It's not accurate, but it is a good indicator.
ReplyDeleteI notice that I have been blocked from viewing David Heffernan's most excellent contributions to these forums. Truly, a great loss indeed.
ReplyDeleteI can share some code if you are interested for Linux. It is actually pretty difficult to do accurately on Posix platforms.
ReplyDeleteAllen Drennan That could come in handy!
ReplyDeleteLars Fosdal I looked again and everything I do uses native OS functions, not the memory manager directly, sorry.
ReplyDeleteAllen 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/sub
ReplyDeleteLars 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
ReplyDeletefunction 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