C# get pc total memory and available memory, process private workingset and workingset64
Install-Package System.Management;
Install-Package System.Diagnostics.PerformanceCounter;
using System.Diagnostics; using System.Management; public class MemoryMonitor { private static PerformanceCounter memoryCounter = null; //Get the current private memory usage in MB. public static int GetPrivateMemoryMB() { if (memoryCounter == null) { memoryCounter=new PerformanceCounter("Process", "Working Set - Private", Process.GetCurrentProcess().ProcessName); } return (int)(memoryCounter.NextValue()/1024f/1024f); } //Gets the total working set memory in MB public static int GetWorkingSetMemoryMB() { using (var proc = Process.GetCurrentProcess()) { return (int)(proc.WorkingSet64/1024f/1024f); } } //Total physical memory public static double GetTotalPhysicalMemory() { using (var searcher = new ManagementObjectSearcher("select TotalPhysicalMemory from win32_computersystem")) { foreach (ManagementObject item in searcher.Get()) { return Convert.ToDouble(item["TotalPhysicalMemory"])/1024f/1024f; } } return 0; } //Available memory public static double GetAvailablePhysicalMemory() { using (var searcher = new ManagementObjectSearcher("select availablebytes from win32_perfformatteddata_perfos_memory")) { foreach (ManagementObject item in searcher.Get()) { return Convert.ToDouble(item["availableBytes"])/1024f/1024f; } } return 0; } }
According to AI
Key Differences: Working Set vs Private Working Set
1. Working Set (WorkingSet64)
using (var process = Process.GetCurrentProcess())
{
long workingSet = process.WorkingSet64; // Total physical RAM used
}
What it measures:
-
Total physical RAM currently allocated to the process
-
Includes both private and shared memory
-
Shared memory = memory-mapped files, DLLs, shared libraries used by multiple processes
-
Represents: The process's current "footprint" in physical RAM
2. Private Working Set (PerformanceCounter)
var privateCounter = new PerformanceCounter("Process", "Working Set - Private",
process.ProcessName);
long privateWorkingSet = privateCounter.NextValue();
What it measures:
-
Exclusively owned memory - cannot be shared with other processes
-
Includes: Heap allocations, stack memory, unique data structures
-
Excludes: Shared DLLs, memory-mapped files, system libraries
-
Represents: The memory cost that would be freed if the process terminated
🎯 Visual Comparison
Process Memory Usage ├── Working Set (Total Physical RAM) │ ├── Private Working Set (40%) - Your app's exclusive memory │ └── Shared Memory (60%) - DLLs, system libraries, shared resources └── Virtual Memory (Disk + RAM)


using System.Diagnostics; using System.Management; namespace ConsoleApp33 { internal class Program { static UInt128 idx = 0; static List<string> guidsList; static object objLock=new object(); static void Main(string[] args) { guidsList = new List<string>(); PopulateListStrings(); } static void PopulateListStrings() { while(++idx>0) { guidsList.Add($"{idx}_{Guid.NewGuid().ToString()}"); if(idx%10000000==0) { string memoryStr = $"Count:{guidsList.Count}\n" + $"Private memory:{MemoryMonitor.GetPrivateMemoryMB()} MB\n" + $"Workingset:{MemoryMonitor.GetWorkingSetMemoryMB()} MB\n"+ $"PC total memory:{MemoryMonitor.GetTotalPhysicalMemory()} MB\n"+ $"PC available memory:{MemoryMonitor.GetAvailablePhysicalMemory()} MB\n"; Console.WriteLine(memoryStr); } } } } public class MemoryMonitor { private static PerformanceCounter memoryCounter = null; //Get the current private memory usage in MB. public static int GetPrivateMemoryMB() { if (memoryCounter == null) { memoryCounter=new PerformanceCounter("Process", "Working Set - Private", Process.GetCurrentProcess().ProcessName); } return (int)(memoryCounter.NextValue()/1024f/1024f); } //Gets the total working set memory in MB public static int GetWorkingSetMemoryMB() { using (var proc = Process.GetCurrentProcess()) { return (int)(proc.WorkingSet64/1024f/1024f); } } //Total physical memory public static double GetTotalPhysicalMemory() { using (var searcher = new ManagementObjectSearcher("select TotalPhysicalMemory from win32_computersystem")) { foreach (ManagementObject item in searcher.Get()) { return Convert.ToDouble(item["TotalPhysicalMemory"])/1024f/1024f; } } return 0; } //Available memory public static double GetAvailablePhysicalMemory() { using (var searcher = new ManagementObjectSearcher("select availablebytes from win32_perfformatteddata_perfos_memory")) { foreach (ManagementObject item in searcher.Get()) { return Convert.ToDouble(item["availableBytes"])/1024f/1024f; } } return 0; } } }

浙公网安备 33010602011771号