如何获取可用系统内存的大小?

发布于 2024-09-10 13:46:55 字数 40 浏览 2 评论 0原文

C#.NET 中是否可以获取系统可用内存的大小?如果是的话怎么办?

Is it possible to get the size of system available memory in C#.NET? if yes how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

岁月如刀 2024-09-17 13:46:55

使用 Microsoft.VisualBasic.Devices.ComputerInfo .总物理内存

右键单击您的项目,添加引用,选择 Microsoft.VisualBasic

Use Microsoft.VisualBasic.Devices.ComputerInfo.TotalPhysicalMemory.

Right-click your project, Add Reference, select Microsoft.VisualBasic.

指尖凝香 2024-09-17 13:46:55

这个答案是基于汉斯·帕桑特的。所需的属性实际上是AvailablePhysicalMemory。它(和 TotalPhysicalMemory 等)是实例变量,所以它应该

new ComputerInfo().AvailablePhysicalMemory

在 C# 中工作,但我想知道为什么 此页面表示对于 C#,“不支持此语言或没有可用的代码示例。”

This answer is based on Hans Passant's. The required property is AvailablePhysicalMemory actually. and it (and TotalPhysicalMemory and others) are instance variables, so it should be

new ComputerInfo().AvailablePhysicalMemory

It works in C#, but I wonder why this page says that for C#, "This language is not supported or no code example is available."

悲歌长辞 2024-09-17 13:46:55

来自 EggHeadCafe 在谷歌搜索“c#系统内存”后,

您需要添加对 System.Management

using System;
using System.Management;

namespace MemInfo
{
    class Program
    {       
        static void Main(string[] args)
        {
            ObjectQuery winQuery = new ObjectQuery("SELECT * FROM Win32_LogicalMemoryConfiguration");

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(winQuery);

            foreach (ManagementObject item in searcher.Get())
            {
                Console.WriteLine("Total Space = " + item["TotalPageFileSpace"]);
                Console.WriteLine("Total Physical Memory = " + item["TotalPhysicalMemory"]);
                Console.WriteLine("Total Virtual Memory = " + item["TotalVirtualMemory"]);
                Console.WriteLine("Available Virtual Memory = " + item["AvailableVirtualMemory"]);
            }
            Console.Read();
        }
    }
}

输出的引用:

总空间 = 4033036

总物理内存 = 2095172

总虚拟内存 = 1933904

可用虚拟内存 = 116280

From EggHeadCafe after googling for 'c# system memory'

You will need to add a reference to System.Management

using System;
using System.Management;

namespace MemInfo
{
    class Program
    {       
        static void Main(string[] args)
        {
            ObjectQuery winQuery = new ObjectQuery("SELECT * FROM Win32_LogicalMemoryConfiguration");

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(winQuery);

            foreach (ManagementObject item in searcher.Get())
            {
                Console.WriteLine("Total Space = " + item["TotalPageFileSpace"]);
                Console.WriteLine("Total Physical Memory = " + item["TotalPhysicalMemory"]);
                Console.WriteLine("Total Virtual Memory = " + item["TotalVirtualMemory"]);
                Console.WriteLine("Available Virtual Memory = " + item["AvailableVirtualMemory"]);
            }
            Console.Read();
        }
    }
}

Output:

Total Space = 4033036

Total Physical Memory = 2095172

Total Virtual Memory = 1933904

Available Virtual Memory = 116280

爱格式化 2024-09-17 13:46:55
var performance = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");
var memory = performance.NextValue();
var performance = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");
var memory = performance.NextValue();
贱人配狗天长地久 2024-09-17 13:46:55

使用通过 System.Diagnostics 访问的性能计数器将是一种选择。

请参阅 http://www.dotnetspider.com/resources /4612-Find-Memory-usage-CPU-usage.aspx

希望这有帮助!

Using the performance counters accessible via System.Diagnostics will be one option.

Refer http://www.dotnetspider.com/resources/4612-Find-Memory-usage-CPU-usage.aspx

Hope this helps!

无声无音无过去 2024-09-17 13:46:55

一段代码:

 System.Diagnostics.PerformanceCounter ramCounter;     
 ramCounter = new System.Diagnostics.PerformanceCounter("Memory", "Available Bytes"); //"Available MBytes" for MB
 string getAvailableRAMInBytes = ramCounter.NextValue() + "byte";

A piece of codes:

 System.Diagnostics.PerformanceCounter ramCounter;     
 ramCounter = new System.Diagnostics.PerformanceCounter("Memory", "Available Bytes"); //"Available MBytes" for MB
 string getAvailableRAMInBytes = ramCounter.NextValue() + "byte";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文