.net - Mono下如何获取可用的虚拟和物理内存大小?

标签 .net mono clr

在 Mono 下运行时,有没有办法获得可用的虚拟和物理内存大小?

最佳答案

你可以使用 "free" command 的这个实现(UNIX) 找出已使用和可用的物理内存(恕我直言,这是最好的选择):

using System;
using System.Text.RegularExpressions;
using System.IO;

namespace FreeCSharp
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            FreeCSharp free = new FreeCSharp();
            free.GetValues();

            long buffersPlusCached = free.Buffers + free.Cached;
            long mainUsed = free.MemTotal - free.MemFree;

            // What you would get from free command:
            Console.WriteLine("-/+ buffers/cache:    {0}     {1}", (mainUsed - buffersPlusCached), (free.MemFree + buffersPlusCached));

            // What means:
            Console.WriteLine("Used physical memory: {0} kB", mainUsed - buffersPlusCached);
            Console.WriteLine("Available physical memory: {0} kB", free.MemFree + buffersPlusCached);
        }
    }

    /// <summary>
    /// FreeCSharp: quick implementation of free command (kind of) using C#
    /// </summary>
    public class FreeCSharp
    {
        public long MemTotal { get; private set; }
        public long MemFree { get; private set; }
        public long Buffers { get; private set; }
        public long Cached { get; private set; }

        public void GetValues()
        {
            string[] memInfoLines = File.ReadAllLines(@"/proc/meminfo");

            MemInfoMatch[] memInfoMatches =
            {
                new MemInfoMatch(@"^Buffers:\s+(\d+)", value => Buffers = Convert.ToInt64(value)),
                new MemInfoMatch(@"^Cached:\s+(\d+)", value => Cached = Convert.ToInt64(value)),
                new MemInfoMatch(@"^MemFree:\s+(\d+)", value => MemFree = Convert.ToInt64(value)),
                new MemInfoMatch(@"^MemTotal:\s+(\d+)", value => MemTotal = Convert.ToInt64(value))
            };

            foreach (string memInfoLine in memInfoLines)
            {
                foreach (MemInfoMatch memInfoMatch in memInfoMatches)
                {
                    Match match = memInfoMatch.regex.Match(memInfoLine);
                    if (match.Groups[1].Success)
                    {
                        string value = match.Groups[1].Value;
                        memInfoMatch.updateValue(value);
                    }
                }
            }
        }

        public class MemInfoMatch
        {
            public Regex regex;
            public Action<string> updateValue;

            public MemInfoMatch(string pattern, Action<string> update)
            {
                this.regex = new Regex(pattern, RegexOptions.Compiled);
                this.updateValue = update;
            }
        }
    }
}

或者,可以使用 sysconf (UNIX) 获取当前可用的物理内存页。该值取决于操作系统缓存了多少数据,因此,您应该执行 echo 3 > /proc/sys/vm/drop_caches在运行此代码之前:
using System;
using Mono.Unix.Native;

OperatingSystem os = Environment.OSVersion;
PlatformID pid = os.Platform;
if (pid == PlatformID.Unix || pid == PlatformID.MacOSX) {
    long pages = Syscall.sysconf (SysconfName._SC_AVPHYS_PAGES);
    long page_size = Syscall.sysconf (SysconfName._SC_PAGESIZE);
    Console.WriteLine("The number of currently available pages of physical memory: {0}, Size of a page in bytes: {1} bytes", pages, page_size);
    Console.WriteLine("Mem: {0} bytes", pages * page_size);
 }

关于.net - Mono下如何获取可用的虚拟和物理内存大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17358345/

相关文章:

.net - .net core 2.0 中的持久身份验证 cookie

mono - 单声道 2.8 的 OpenID

c# - 如何以可移植的方式在 C# 中播放压缩的声音文件?

c# - 有没有内存IL重写的例子(Profiler API)?

clr - .Net 4.0 会包含新的 CLR 还是保留 2.0 版

.net - 每个优秀的 .NET 开发人员都应该能够回答的问题?

c# - iTextSharp 的 Rtf 支持哪里去了

c# - 如何允许用户编辑 Program Files 下的 App.config 是记事本或其他编辑器?

c# - mono c# strange Address already in use 错误

c# - 防止非托管函数指针垃圾回收