示例#1
0
        /// <summary>Linux specific implementation of <see cref="Memory.GetRamUsageInfo"/> </summary>
        internal static int GetRamUsageInfo(ref RamUsageInfo buffer)
        {
            try
            {
                string[] lines            = System.IO.File.ReadAllLines("/proc/meminfo");
                string   memTotalLine     = lines.FirstOrDefault(line => line.StartsWith("MemTotal:"));
                string   memAvailableLine = lines.FirstOrDefault(line => line.StartsWith("MemAvailable:"));
                buffer.TotalBytes = ExtractValue(memTotalLine) * 1024;
                buffer.FreeBytes  = ExtractValue(memAvailableLine) * 1024;
                return(0);
            }
            #pragma warning disable
            catch (IOException)
            {
                return(ERROR);
            }
            #pragma warning restore

            ulong ExtractValue(string line)
            {
                return(line != null && ulong.TryParse(line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1], out var val)
                    ? val
                    : 0);
            }
        }
示例#2
0
        internal static int GetRamUsageInfo(ref RamUsageInfo buffer)
        {
            var buf = new MacRamUsageInfo();
            var ret = GetRamUsageInfo(ref buf, Marshal.SizeOf(buf));

            if (ret != 0)
            {
                return(ERROR);
            }
            buffer.TotalBytes = s_totalMemoryBytes.Value;
            buffer.FreeBytes  = s_totalMemoryBytes.Value - (buf.AppMemory + buf.Wired + buf.Compressed);
            return(0);
        }
示例#3
0
        /// <summary>Linux specific implementation of <see cref="Memory.GetRamUsageInfo"/> </summary>
        internal static int GetRamUsageInfo(ref RamUsageInfo buffer)
        {
            var sysinfoBuf = new sysinfo_buf();
            var ret        = sysinfo(ref sysinfoBuf);

            if (ret != 0)
            {
                return(ERROR);
            }
            buffer.TotalBytes = sysinfoBuf.totalram;
            buffer.FreeBytes  = sysinfoBuf.freeram;
            return(0);
        }
示例#4
0
        private double?GetAvailablePhysicalBytesMacOS(ulong totalPhysicalBytes)
        {
            double?      availableAvailablePhysicalBytes = null;
            RamUsageInfo ramUsageInfo = new RamUsageInfo();

            // The 'free' portion of GetRamUsageInfo() indicates how many physical RAM is still available,
            // we have to look at VM statistics to calculate the actual available RAM though
            if (GetRamUsageInfo(ref ramUsageInfo) == MACOS_INTEROP_SUCCESS)
            {
                availableAvailablePhysicalBytes = totalPhysicalBytes
                                                  - ramUsageInfo.AppMemory
                                                  - ramUsageInfo.Wired
                                                  - ramUsageInfo.Compressed;
            }

            return(availableAvailablePhysicalBytes);
        }
示例#5
0
 /// <summary>
 /// Linux specific implementation of <see cref="Memory.GetRamUsageInfo"/> \
 /// </summary>
 internal static int GetRamUsageInfo(ref RamUsageInfo buffer)
 {
     try
     {
         string[] lines            = System.IO.File.ReadAllLines($"{ProcPath}{ProcMemInfoPath}");
         string   memTotalLine     = lines.FirstOrDefault(line => line.StartsWith("MemTotal:"));
         string   memAvailableLine = lines.FirstOrDefault(line => line.StartsWith("MemAvailable:"));
         buffer.TotalBytes = ExtractValueFromProcLine(memTotalLine) * 1024;
         buffer.FreeBytes  = ExtractValueFromProcLine(memAvailableLine) * 1024;
         return(0);
     }
     #pragma warning disable
     catch (Exception)
     {
         return(ERROR);
     }
     #pragma warning restore
 }
示例#6
0
        private double?GetAvailablePhysicalBytesMacOS()
        {
            double?      availableAvailablePhysicalBytes = null;
            RamUsageInfo ramUsageInfo = new RamUsageInfo();

            unsafe
            {
                if (GetRamUsageInfo(&ramUsageInfo) == MACOS_INTEROP_SUCCESS)
                {
                    // The 'free' portion of GetRamUsageInfo() indicates how many physical RAM is still available,
                    // we have to look at VM statistics to calculate the actual available RAM though, akin to ActivityMonitor on macOS.
                    availableAvailablePhysicalBytes =
                        OperatingSystemHelper.GetPhysicalMemorySize().Bytes - (ramUsageInfo.Active + ramUsageInfo.Speculative +
                                                                               ramUsageInfo.Wired + ramUsageInfo.Compressed - ramUsageInfo.Purgable);
                }
            }

            return(availableAvailablePhysicalBytes);
        }
示例#7
0
        private double?GetAvailablePhysicalBytesMacOS(ulong totalPhysicalBytes)
        {
            double?      availableAvailablePhysicalBytes = null;
            RamUsageInfo ramUsageInfo = new RamUsageInfo();

            // The 'free' portion of GetRamUsageInfo() indicates how many physical RAM is still available,
            // we have to look at VM statistics to calculate the actual available RAM though
            if (GetRamUsageInfo(ref ramUsageInfo) == MACOS_INTEROP_SUCCESS)
            {
                // OLD formula based on Active pages (does not coincide with Activity Monitor)
                //availableAvailablePhysicalBytes =
                //    totalPhysicalBytes - (ramUsageInfo.Active + ramUsageInfo.Speculative +
                //        ramUsageInfo.Wired + ramUsageInfo.Compressed - ramUsageInfo.Purgable);

                // "Physical Memory" - "Memory Used" from Activity Monitor
                availableAvailablePhysicalBytes = totalPhysicalBytes
                                                  - ramUsageInfo.AppMemory
                                                  - ramUsageInfo.Wired
                                                  - ramUsageInfo.Compressed;
            }

            return(availableAvailablePhysicalBytes);
        }
示例#8
0
 /// <summary>
 /// Returns the current host memory usage information to the caller
 /// </summary>
 /// <param name="buffer">A RamUsageInfo struct pointer to hold memory statistics</param>
 public static int GetRamUsageInfo(ref RamUsageInfo buffer) => IsMacOS
     ? Impl_Mac.GetRamUsageInfo(ref buffer)
     : Impl_Linux.GetRamUsageInfo(ref buffer);
示例#9
0
 /// <summary>
 /// Returns the current host memory usage information to the caller
 /// </summary>
 /// <param name="buffer">A RamUsageInfo struct pointer to hold memory statistics</param>
 public static int GetRamUsageInfo(ref RamUsageInfo buffer)
 => GetRamUsageInfo(ref buffer, Marshal.SizeOf(buffer));
示例#10
0
 private static extern int GetRamUsageInfo(ref RamUsageInfo buffer, long bufferSize);