[ACCEPTED]-How can I get the total physical memory in C#?-memory-management

Accepted answer
Score: 12

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 1 changed

GlobalMemoryStatusEx(ref statEX);

To

GlobalMemoryStatusEx(statEX);

It work correctly. Tanks

Score: 7

How about:

My.Computer.Info.TotalPhysicalMemory
My.Computer.Info.AvailablePhysicalMemory

0

Score: 5

If c# you can:

Reference the Microsoft.VisualBasic assembly. Then 2 import Microsoft.VisualBasic.Devices namespace.
And finally use ComputerInfo 1 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";
Score: 1

you can use this templates:

long memory = Process.GetCurrentProcess().PeakVirtualMemorySize64;

And another properties 1 with names Peak*64

Score: 0

You forgot to set statEX.dwLength before calling GlobalMemoryStatusEx.

MEMORYSTATUSEX statEX = new MEMORYSTATUSEX();
statEX.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
GlobalMemoryStatusEx(ref statEX);

0

More Related questions