Example #1
0
        /// <summary>
        /// 获取Windows的内存指标
        /// </summary>
        /// <returns></returns>
        public MemoryMetrics GetWindowsMetrics()
        {
            var output = "";

            var info = new ProcessStartInfo
            {
                FileName  = "wmic",
                Arguments = "OS get FreePhysicalMemory,TotalVisibleMemorySize /Value",
                RedirectStandardOutput = true
            };

            using (var process = Process.Start(info))
            {
                output = process.StandardOutput.ReadToEnd();
            }

            var lines = output.Trim().Split(new string[1] {
                "\n"
            }, StringSplitOptions.RemoveEmptyEntries);
            var freeMemoryParts = lines[0].Split(new string[1] {
                "="
            }, StringSplitOptions.RemoveEmptyEntries);
            var totalMemoryParts = lines[1].Split(new string[1] {
                "="
            }, StringSplitOptions.RemoveEmptyEntries);

            var metrics = new MemoryMetrics();

            metrics.Total = Math.Round(double.Parse(totalMemoryParts[1]) / 1024, 0);
            metrics.Free  = Math.Round(double.Parse(freeMemoryParts[1]) / 1024, 0);
            metrics.Used  = metrics.Total - metrics.Free;

            return(metrics);
        }
Example #2
0
        /// <summary>
        /// 获取Unix的内存指标
        /// </summary>
        /// <returns></returns>
        public MemoryMetrics GetUnixMetrics()
        {
            var output = "";

            var info = new ProcessStartInfo("free -m")
            {
                FileName  = "/bin/bash",
                Arguments = "-c \"free -m\"",
                RedirectStandardOutput = true
            };

            using (var process = Process.Start(info))
            {
                output = process.StandardOutput.ReadToEnd();
                Console.WriteLine(output);
            }

            var lines = output.Split(new string[1] {
                "\n"
            }, StringSplitOptions.RemoveEmptyEntries);
            var memory = lines[1].Split(new string[1] {
                " "
            }, StringSplitOptions.RemoveEmptyEntries);

            var metrics = new MemoryMetrics();

            metrics.Total = double.Parse(memory[1]);
            metrics.Used  = double.Parse(memory[2]);
            metrics.Free  = double.Parse(memory[3]);

            return(metrics);
        }