7

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

4

2 回答 2

3

您可以使用“free”命令(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);
 }
于 2014-07-08T20:58:38.083 回答
0

我没有使用太多 Mono,但是您是否尝试过查看Mono Profiler日志?我知道他们显示物理内存,但我不确定虚拟...

叫我好奇,但你为什么需要这些?如果您在应用程序中遇到内存使用问题,您可以使用许多不同的技术来帮助减少它......

编辑:环顾四周,确实没有一种特定的方式来收集它......但是,根据您运行应用程序的方式,您仍然可以访问 C# 中的当前进程对象

Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
var physicalMemory = currentProcess.WorkingSet64;
var virtualMemory = currentProcess.PeakVirtualMemorySize64;

然后只需使用/返回您认为合适的那些。

来源:MSDN

于 2014-07-07T18:44:12.220 回答