获取cpu和内存使用情况

public class SystemInfo
    {
        [DllImport("kernel32")]
        public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
        [DllImport("kernel32")]
        public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
        [DllImport("kernel32")]
        public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
        [DllImport("kernel32")]
        public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);

        PerformanceCounter cpu; MEMORY_INFO MemInfo;

        public SystemInfo()
        {
            cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");

            MemInfo = new MEMORY_INFO();
        }

        public float[] GetSystemInfo()
        {
            float[] ret = new float[2];

            GlobalMemoryStatus(ref MemInfo);
            ret[1] = MemInfo.dwMemoryLoad;
            ret[0] = cpu.NextValue();

            return ret;
        }
    }

    //定义CPU的信息结构
    [StructLayout(LayoutKind.Sequential)]
    public struct CPU_INFO
    {
        public uint dwOemId;
        public uint dwPageSize;
        public uint lpMinimumApplicationAddress;
        public uint lpMaximumApplicationAddress;
        public uint dwActiveProcessorMask;
        public uint dwNumberOfProcessors;
        public uint dwProcessorType;
        public uint dwAllocationGranularity;
        public uint dwProcessorLevel;
        public uint dwProcessorRevision;
    }
    //定义内存的信息结构
    [StructLayout(LayoutKind.Sequential)]
    public struct MEMORY_INFO
    {
        public uint dwLength;
        public uint dwMemoryLoad;
        public uint dwTotalPhys;
        public uint dwAvailPhys;
        public uint dwTotalPageFile;
        public uint dwAvailPageFile;
        public uint dwTotalVirtual;
        public uint dwAvailVirtual;
    }
    //定义系统时间的信息结构
    [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEMTIME_INFO
    {
        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMilliseconds;
    }

原文地址:https://www.cnblogs.com/microstep/p/9479590.html

时间: 2024-07-31 04:48:19

获取cpu和内存使用情况的相关文章

Python 系统资源信息获取CPU 和内存使用情况

In [6]: psutil.swap_memory()Out[6]: sswap(total=23501340672, used=20144758784, free=3356581888, percent=85.7, sin=0, sout=0) In [7]: psutil.cpu_percent(interval=None, percpu=False)Out[7]: 32.8 原文地址:https://www.cnblogs.com/du-jun/p/11984608.html

获取rds的cpu和内存使用情况

python版本:2.7.12 #!/usr/bin/env python #coding=utf-8 import time import os, json, urllib, datetime, shutil from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.e

方法:Linux 下用JAVA获取CPU、内存、磁盘的系统资源信息

CPU使用率: InputStream is = null; InputStreamReader isr = null; BufferedReader brStat = null; StringTokenizer tokenStat = null; // 用来分隔String的应用类 try { System.out.println("Get usage rate of CUP : "); Process process = Runtime.getRuntime().exec(&quo

VMware ESXi中不能显示CPU及内存使用情况的解决方法

今天一个网友问我,他管理的机房有4台ESXi 5.1的服务器,其中三台ESXi Server不能显示各个虚拟机占用的CPU.内存情况了,如图1-1所示. 图1-1在"虚拟机"选项卡中不能显示每个启动虚拟机的资源占用情况 另外,在VMware ESXi的"摘要"中,CPU与内存的使用情况也统计出错,如图1-2所示. 图1-2 摘要统计出错 在出现这个问题时,各个ESXi Server上的虚拟机可以正常启动.关闭,并且各虚拟机运行的系统及应用不受影响. 从上面两个图我看

Android获取cpu和内存信息、网址的代码

android获取手机cpu并判断是单核还是多核 /** * Gets the number of cores available in this device, across all processors. * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu" * @return The number of cores, or 1 if failed to get result */ pri

Linux查看CPU和内存使用情况总结

Linux查看CPU和内存使用情况:http://www.cnblogs.com/xd502djj/archive/2011/03/01/1968041.html 在做Linux系统优化的时候,物理内存是其中最重要的一方面.自然的,Linux也提供了非常多的方法来监控宝贵的内存资源的使用情况.下面的清单详细的列出了Linux系统下通过视图工具或命令行来查看内存使用情况的各种方法. 1. /proc/meminfo 查看RAM使用情况最简单的方法是通过/proc/meminfo.这个动态更新的虚拟

戴尔R740服务器获取cpu、内存、硬盘参数信息。

戴尔R740服务器获取cpu.内存.硬盘参数信息.使用redfish协议,只使用了system的一个总URL即可获取所有参数. import requests import json requests.packages.urllib3.disable_warnings() ##使用一个system总的URL分别获取到cpu.内存.存储三个url.所以只修改system的URL即可 ##sel日志单独使用URL获取 class GetHostInfo(object): def __init__(s

Linux查看CPU和内存使用情况详解

在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中, 可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态会以全屏的方式显示,并且会处在对话的 模式 -- 用基于 top 的命令,可以控制显示方式等等.退出 top 的命令为 q (在 top 运行中敲 q 键一次). 1.top top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务 管理器.可

Linux查看CPU和内存使用情况

在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态会以全屏的方式显示,并且会处在对话的模式 -- 用基于 top 的命令,可以控制显示方式等等.退出 top 的命令为 q (在 top 运行中敲 q 键一次). top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器 可以直接使用top命