Determining the virtual size of a process using delphi

I have a Delphi program, and I’m watching how this program can print its own “virtual size” in a log file so that I can see when it used too much memory. How to determine virtual size using Delphi code?

By "virtual size" I mean the value displayed by Process Explorer . This value cannot be displayed by the normal task manager. This is not just the use of memory by the program, but the use of address space. In Win32, a program cannot use more than 2 GB of address space.

PS: I am using Delphi 6, but the code / information for other versions should also be in order.

+6
source share
3 answers

Thanks to this post, which gives hints on how to get virtual size using C / C ++, I was able to write the following Delphi function:

Type TMemoryStatusEx = packed record dwLength: DWORD; dwMemoryLoad: DWORD; ullTotalPhys: Int64; ullAvailPhys: Int64; ullTotalPageFile: Int64; ullAvailPageFile: Int64; ullTotalVirtual: Int64; ullAvailVirtual: Int64; ullAvailExtendedVirtual: Int64; end; TGlobalMemoryStatusEx = function(var MSE: TMemoryStatusEx): LongBool; stdcall; function VirtualSizeUsage: Int64; var MSE: TMemoryStatusEx; fnGlobalMemoryStatusEx: TGlobalMemoryStatusEx; begin Result := 0; @fnGlobalMemoryStatusEx := GetProcAddress(GetModuleHandle(kernel32), 'GlobalMemoryStatusEx'); if Assigned(@fnGlobalMemoryStatusEx) then begin MSE.dwLength := SizeOf(MSE); if fnGlobalMemoryStatusEx(MSE) then Result := MSE.ullTotalVirtual-MSE.ullAvailVirtual; end; end; 

It seems to work fine for me (Delphi 6, Win XP). It may be easier to find a GlobalMemoryStatus solution instead of a GlobalMemoryStatusEx but it will not work correctly on systems with more than 2 GB of memory.

+10
source

The Process Explorer seems to do this by calling NtQueryInformation , but you can also use performance data, see GetProcessVirtualBytes in my answer here .

+6
source

And for those who are already dependent on the excellent Jedi Code Library , you can find definitions that @Name matches the above in JclWin32 .

The actual numbers you need are also broken down into individual functions in JclSysInfo . Just call GetTotalVirtualMemory () - GetFreeVirtualMemory () to do the calculations.

+1
source

Source: https://habr.com/ru/post/901612/


All Articles