Last active
July 26, 2024 17:49
-
-
Save kforeverisback/75ff24129e7bd8208e8edb1052d002ff to your computer and use it in GitHub Desktop.
Total Memory in C# Interop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a sample output in my machine with 32GB of RAM.
As you can see, the GlobalMemoryStatusEx reports different memory. And this is also confirmed by this documentation.