-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
Background and motivation
The System.Environment class has many useful properties regarding the environment where the process runs, but it doesn't currently have properties about the total and free amount of physical and virtual memories. These are very important metrics, especially for computation-intensive projects like ML.NET's machine learning.
It would be great if the Environment class would have more properties describing the total and free amounts of physical and virtual memories.
API Proposal
namespace System
{
public static partial class Environment
{
public static long? TotalPhysicalMemory { get; }
public static long? FreePhysicalMemory { get; }
public static long? TotalVirtualMemory { get; }
public static long? FreeVirtualMemory { get; }
}
}API Usage
// Get the total physical memory (in bytes)
var totalRam = Environment.TotalPhysicalMemory;
// Get the total physical memory (in bytes)
var freeRam = Environment.FreePhysicalMemory ;
// Get the total physical memory (in bytes)
var totalVM = Environment.TotalVirtualMemory;
// Get the total physical memory (in bytes)
var freeVM = Environment.FreeVirtualMemory;Alternative Designs
if (OperatingSystem.IsWindows())
{
ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get();
foreach (ManagementObject result in results)
{
ulong totalVisibleMemorySize = (ulong)result["TotalVisibleMemorySize"];
ulong freePhysicalMemory = (ulong)result["FreePhysicalMemory"];
ulong totalVirtualMemorySize = (ulong)result["TotalVirtualMemorySize"];
ulong freeVirtualMemory = (ulong)result["FreeVirtualMemory"];
}
}Risks
It's not a risk per say, but these new properties would require some extra work, because we would need to call platform-specific APIs. Virtual memory is used on Windows, Linux, Android and iOS, so the appropriate APIs should be available on those, but I'm not sure about others that .NET supports.
I would define these new properties as nullable types, because I can image that some .NET supported operating system doesn't support these reports regarding memory usage.
Also, if it's not implemented on a specific platform, they could return null.