Skip to content

Instantly share code, notes, and snippets.

@kforeverisback
Last active July 26, 2024 17:49
Show Gist options
  • Save kforeverisback/75ff24129e7bd8208e8edb1052d002ff to your computer and use it in GitHub Desktop.
Save kforeverisback/75ff24129e7bd8208e8edb1052d002ff to your computer and use it in GitHub Desktop.
Total Memory in C# Interop
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace memory_test
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
class MEMORYSTATUSEX
{
public uint dwLength;
public uint dwMemoryLoad;
public ulong ullTotalPhys;
public ulong ullAvailPhys;
public ulong ullTotalPageFile;
public ulong ullAvailPageFile;
public ulong ullTotalVirtual;
public ulong ullAvailVirtual;
public ulong ullAvailExtendedVirtual;
public MEMORYSTATUSEX()
{
this.dwLength = (uint) Marshal.SizeOf(typeof( MEMORYSTATUSEX ));
}
}
static class Program
{
[DllImport("kernel32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetPhysicallyInstalledSystemMemory(out ulong MemoryInKilobytes);
[DllImport("kernel32.dll", SetLastError=true, EntryPoint="GetPhysicallyInstalledSystemMemory")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetPhysicallyInstalledSystemMemory_long(out long MemoryInKilobytes);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool GlobalMemoryStatusEx( [In,Out] MEMORYSTATUSEX lpBuffer);
static void Main(string[] args)
{
Console.WriteLine("Getting data using GetPhysicallyInstalledSystemMemory");
ulong memoryInKilobytes;
GetPhysicallyInstalledSystemMemory(out memoryInKilobytes);
Console.WriteLine((memoryInKilobytes / 1024) + "MB of RAM installed - ulong.");
long memoryInKB;
GetPhysicallyInstalledSystemMemory_long(out memoryInKB);
Console.WriteLine((memoryInKB / 1024) + "MB of RAM installed - long.");
Console.WriteLine("Getting data using GlobalMemoryStatusEx");
var mstatex = new MEMORYSTATUSEX();
if (GlobalMemoryStatusEx(mstatex))
{
Console.WriteLine((mstatex.ullTotalPhys / (1024*1024)) + "MB of Total Phys.");
Console.WriteLine((mstatex.ullAvailPhys / (1024*1024)) + "MB of Available Phys.");
Console.WriteLine((mstatex.ullTotalVirtual / (1024*1024)) + "MB of Total Virtual.");
//etc.. Repeat for other structure members
}
else
{
// Use a more appropriate Exception Type. 'Exception' should almost never be thrown
throw new Exception("Unable to initalize the GlobalMemoryStatusEx API");
}
}
}
}
@kforeverisback
Copy link
Author

Here is a sample output in my machine with 32GB of RAM.

Getting data using GetPhysicallyInstalledSystemMemory
32768MB of RAM installed - ulong.
32768MB of RAM installed - long.
Getting data using GlobalMemoryStatusEx
32354MB of Total Phys installed.
8516MB of Available Phys installed.
134217727MB of Total Virtual installed.

As you can see, the GlobalMemoryStatusEx reports different memory. And this is also confirmed by this documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment