How do you get total amount of RAM the computer has?
The Windows API function GlobalMemoryStatusEx
can be called with p/invoke:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private 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(NativeMethods.MEMORYSTATUSEX));
}
}
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);
Then use like:
ulong installedMemory;
MEMORYSTATUSEX memStatus = new MEMORYSTATUSEX();
if( GlobalMemoryStatusEx( memStatus))
{
installedMemory = memStatus.ullTotalPhys;
}
Or you can use WMI (managed but slower) to query TotalPhysicalMemory
in the Win32_ComputerSystem
class.
Get total installed RAM in C# on Windows 10
The TotalPhysicalMemory
is expressed in bytes.
If you want the memory to be converted to GB use this sample:
Convert.ToInt32(InstalledRam/(1024*1024*1024));
Retrieving Total amount of RAM on a computer
This information is already available directly in the .NET framework, you might as well use it. Project + Add Reference, select Microsoft.VisualBasic.
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("You have {0} bytes of RAM",
new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory);
Console.ReadLine();
}
}
And no, it doesn't turn your C# code into vb.net.
How I can get the installed RAM and CPU details c# .Net Core?
Looks like you're using the wrong class. Try the Win32_PhysicalMemory
class and the Capacity
property
var query = "SELECT Capacity FROM Win32_PhysicalMemory";
var searcher = new ManagementObjectSearcher(query);
foreach (var WniPART in searcher.Get())
{
var capacity = Convert.ToUInt64(WniPART.Properties["Capacity"].Value);
var capacityKB = capacity / 1024;
var capacityMB = capacityKB / 1024;
var capacityGB = capacityMB / 1024;
System.Console.WriteLine("Size in KB: {0}, Size in MB: {1}, Size in GB: {2}", capacityKB, capacityMB, capacityGB);
}
The MaxCapacity
property in the Win32_PhysicalMemoryArray
class will give you the maximum amount of RAM installable on your machine, not the amount of memory that is actually installed.
How to know the total amount of RAM of a computer?
With Node.js OS package you can do this and much more.
Take a look in os.freemem or os.totalmem
From Node.js documentation:
os.freemem()
Returns the amount of free system memory in bytes.
os.totalmem()
Returns the total amount of system memory in bytes.
How to get total memory/RAM in GO?
Related Topics
Model Id Property Null in ASP.NET MVC C#
How to Get a Specific Column Value from a Datatable
Cast Class into Another Class or Convert Class to Another
Select Distinct by Two Properties in a List
Response to Preflight Request Doesn't Pass Access Control Check (Angular2)
A Better Way to Use Automapper to Flatten Nested Objects
How to Convert HTML to Pdf Using Itextsharp
Redirecting to Another Page in ASP.NET MVC Using Javascript/Jquery
How to Determine If a Json Object Contains Only a Specific Key
Formatting Date in Linq-To-Entities Query Causes Exception
Smtpexception: Unable to Read Data from the Transport Connection: Net_Io_Connectionclosed
How to Get Cookies Info Inside of a Cookiecontainer (All of Them, Not for a Specific Domain)
Display Label Text With Line Breaks in C#
The Ssl Connection Could Not Be Established