Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the total physical memory in C#?

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 don't think this function works in my Windows 7 environment.

    [StructLayout(LayoutKind.Sequential)]
    internal struct MEMORYSTATUSEX
    {
        internal uint dwLength;
        internal uint dwMemoryLoad;
        internal ulong ullTotalPhys;
        internal ulong ullAvailPhys;
        internal ulong ullTotalPageFile;
        internal ulong ullAvailPageFile;
        internal ulong ullTotalVirtual;
        internal ulong ullAvailVirtual;
        internal ulong ullAvailExtendedVirtual;
    }
    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    internal static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);

    private void btnGlobalMemoryStatusEX_Click(object sender, EventArgs e)
    {
        MEMORYSTATUSEX statEX = new MEMORYSTATUSEX();
        GlobalMemoryStatusEx(ref statEX);

        double d = (double)statEX.ullTotalPhys;
    }

Can anybody tell me where I went wrong with wrong code?

like image 733
Amir Borzoei Avatar asked Oct 12 '09 08:10

Amir Borzoei


People also ask

How can I find out how much memory is available in C?

These two glibc extensions should give you the available number of pages. We can then just multiply that by the pages size sysconf(_SC_PAGESIZE) to find the total available memory in bytes.

How do I check my total memory?

Press Ctrl + Shift + Esc to launch Task Manager. Or, right-click the Taskbar and select Task Manager. Select the Performance tab to see current RAM usage displayed in the Memory box, and total RAM capacity listed under Physical Memory.

What is total physical memory?

The total amount of physical memory on a computer depends on how many sticks of RAM are installed and their storage capacity. For example, if a computer has two 64 MB memory modules installed, it has a total of 128 MB of physical memory.

What is physical memory usage?

Physical Memory: The amount of RAM installed. Memory Used: The amount of RAM being used. To the right, you can see where the memory is allocated. App Memory: The amount of memory being used by apps. Wired Memory: Memory required by the system to operate.


2 Answers

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

like image 191
Amir Borzoei Avatar answered Oct 03 '22 06:10

Amir Borzoei


How about:

My.Computer.Info.TotalPhysicalMemory
My.Computer.Info.AvailablePhysicalMemory
like image 25
Bobby Avatar answered Oct 03 '22 08:10

Bobby