I am using the GlobalMemoryStatusEx
function to retrieve information about memory, but this function doesn\'t work correctly. It returns 0 for all properties. I
you can use this templates:
long memory = Process.GetCurrentProcess().PeakVirtualMemorySize64;
And another properties with names Peak*64
How about:
My.Computer.Info.TotalPhysicalMemory
My.Computer.Info.AvailablePhysicalMemory
You forgot to set statEX.dwLength
before calling GlobalMemoryStatusEx
.
MEMORYSTATUSEX statEX = new MEMORYSTATUSEX();
statEX.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
GlobalMemoryStatusEx(ref statEX);
I find my mistake from: http://www.pinvoke.net/default.aspx/kernel32/GlobalMemoryStatusEx.html
I Changed
internal static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);
To
static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);
and changed
GlobalMemoryStatusEx(ref statEX);
To
GlobalMemoryStatusEx(statEX);
It work correctly. Tanks
If c# you can:
Reference the Microsoft.VisualBasic
assembly.
Then import Microsoft.VisualBasic.Devices
namespace.
And finally use ComputerInfo to get the total physical memory.
int bytesPerMebibyte = (1 << 20); // http://physics.nist.gov/cuu/Units/binary.html
ComputerInfo myCompInfo = new ComputerInfo();
string physicalMemory = "Physical Memory: "
+ (myCompInfo.TotalPhysicalMemory / bytesPerMebibyte) + " MB";