How to Get the amount of RAM available on the System in C#
By FoxLearn 2/10/2025 2:03:32 AM 18.76K
How to Get Available Memory in C#?
In C#, you can get the amount of available RAM on a system using the Microsoft.VisualBasic.Devices
namespace or by querying the system's performance counters.
First, Open your Visual Studio, then create a new Console Application project to play demo.
If you want to use Microsoft.VisualBasic.Devices
you need to add a reference to Microsoft.VisualBasic
if it's not already included in your project, then use the ComputerInfo
class to get the available physical memory.
The ComputerInfo
class in the Microsoft.VisualBasic.Devices
namespace provides information about the computer's memory.
C# Get Available Memory
In C#, you can get the total physical memory (RAM size) of the system using the ComputerInfo
class from the Microsoft.VisualBasic.Devices
namespace.
For example, c# get ram size
ComputerInfo computerInfo = new ComputerInfo(); // c# get free memory ulong availableMemory = computerInfo.AvailablePhysicalMemory; // c# get memory available Console.WriteLine($"Available Physical Memory: {availableMemory / 1024 / 1024} MB");
If you want to use PerformanceCounter
you should add a reference to System.Diagnostics
to your project, then use the PerformanceCounter
class to get the available memory.
For example, using PerformanceCounter
to get available memory in c#
// Create a PerformanceCounter for available memory PerformanceCounter availableMemory = new PerformanceCounter("Memory", "Available MBytes"); // Get the available memory float availableMB = availableMemory.NextValue(); Console.WriteLine($"Available Physical Memory: {availableMB} MB");
The PerformanceCounter
class can be used to get various system performance metrics, including available memory.
C# Get Ram Usage
In C#, you can get the RAM usage of the system by using the System.Diagnostics
namespace and the PerformanceCounter
class or the System.Diagnostics.Process
class.
For example, Using PerformanceCounter
for RAM Usage
// Create a PerformanceCounter to get the memory usage in MB PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes"); float availableMemory = ramCounter.NextValue(); // Get the total physical memory // c# get total memory ulong totalMemory = (new Microsoft.VisualBasic.Devices.ComputerInfo()).TotalPhysicalMemory; // Calculate used memory float usedMemory = (totalMemory / 1024 / 1024) - availableMemory; // Convert bytes to MB Console.WriteLine("Total RAM: " + (totalMemory / 1024 / 1024) + " MB"); Console.WriteLine("Used RAM: " + usedMemory + " MB");
In this example:
- The
"Memory"
category provides information about system memory, and"Available MBytes"
gives the free memory. - You can calculate the used memory by subtracting the available memory from the total physical memory.
If you want to get the RAM usage of the current application, you can use the Process
class.
For example, Using Process
to Get RAM Usage of the Current Application
Process currentProcess = Process.GetCurrentProcess(); // Get the memory used by the current process in bytes long memoryUsageInBytes = currentProcess.PrivateMemorySize64; // Convert bytes to MB double memoryUsageInMB = memoryUsageInBytes / (1024 * 1024); Console.WriteLine("Memory Usage by Current Process: " + memoryUsageInMB + " MB");
The code above retrieves the amount of memory being used by the current process (PrivateMemorySize64
), which gives you a sense of how much RAM your application is consuming.
If you want to see how much memory is being used by the .NET runtime for your application, you can use the GC.GetTotalMemory()
method:
For example, Using GC.GetTotalMemory()
for Memory Used by the .NET runtime
// Get the total memory used by the application long memoryUsed = GC.GetTotalMemory(false); // Convert bytes to MB double memoryUsedInMB = memoryUsed / (1024 * 1024); Console.WriteLine("Memory Used by .NET runtime: " + memoryUsedInMB + " MB");
C# Get Total Memory
If you want to get information about the ram available on your system, you can use the Win32_OperatingSystem WMI class.
You need to import System.Management to your project by right-clicking on your project, then select Add References.
Openning your Program class, then modify your code as shown below.
For example, c# get total memory
// get total memory in c# static void Main(string[] args) { ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_OperatingSystem"); ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(objectQuery); ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get(); foreach (ManagementObject managementObject in managementObjectCollection) { Console.WriteLine("Total Visible Memory: {0} KB", managementObject["TotalVisibleMemorySize"]); Console.WriteLine("Free Physical Memory: {0} KB", managementObject["FreePhysicalMemory"]); Console.WriteLine("Total Virtual Memory: {0} KB", managementObject["TotalVirtualMemorySize"]); Console.WriteLine("Free Virtual Memory: {0} KB", managementObject["FreeVirtualMemory"]); } Console.ReadLine(); }
Press F5 to run your project.
TotalVisibleMemorySize: The amount of physical memory available for the operating system. This value does not necessarily indicate the actual physical memory capacity, but what is reported for the operating system is available to it.
FreePhysicalMemory: The amount of physical memory currently not in use and available.
TotalVirtualMemorySize: Capacity of virtual memory. Calculated by adding the total amount of RAM to the paging capacity, that is, adding the amount of memory in or aggregated by the computer system to the SizeStoredInPagingFiles property.
FreeVirtualMemory: Virtual memory capacity currently unused and available.
- How to Download and Install Metro Framework
- How to undo the changes in Entity Framework
- How to create a Metro Message Box in C#
- How to Create a Metro GridView in C#
- How to use Modern UI Metro Framework in C#
- Object Services Layer in Entity Framework
- Improving Entity Framework performance
- Entity Framework Code First vs Database First vs Model First Approach