编程获取系统当前cpu使用率/空闲率 、内存使用率、剩余可用内存等
2009-09-11 11:28
671 查看
编程获取系统当前cpu使用率/空闲率 、内存使用率、剩余可用内存等
2008年06月05日 星期四 16:58
我写的一个cpu使用率例子:
#define _WIN32_WINNT 0x0501
#include <Windows.h>
#include <iostream>
using namespace std;
__int64 CompareFileTime ( FILETIME time1, FILETIME time2 )
{
__int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime ;
__int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime ;
return (b - a);
}
void main()
{
HANDLE hEvent;
BOOL res ;
FILETIME preidleTime;
FILETIME prekernelTime;
FILETIME preuserTime;
FILETIME idleTime;
FILETIME kernelTime;
FILETIME userTime;
res = GetSystemTimes( &idleTime, &kernelTime, &userTime );
preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime ;
hEvent = CreateEvent (NULL,FALSE,FALSE,NULL); // 初始值为 nonsignaled ,并且每次触发后自动设置为nonsignaled
while (1){
WaitForSingleObject( hEvent,1000 ); //等待500毫秒
res = GetSystemTimes( &idleTime, &kernelTime, &userTime );
int idle = CompareFileTime( preidleTime,idleTime);
int kernel = CompareFileTime( prekernelTime, kernelTime);
int user = CompareFileTime(preuserTime, userTime);
int cpu = (kernel +user - idle) *100/(kernel+user);
int cpuidle = ( idle) *100/(kernel+user);
cout << "CPU利用率:" << cpu << "%" << " CPU空闲率:" <<cpuidle << "%" <<endl;
preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime ;
}
}
运行效果如图:
MSDN中 获取内存使用情况的例子:
// Sample output:
// There is 51 percent of memory in use.
// There are 2029968 total Kbytes of physical memory.
// There are 987388 free Kbytes of physical memory.
// There are 3884620 total Kbytes of paging file.
// There are 2799776 free Kbytes of paging file.
// There are 2097024 total Kbytes of virtual memory.
// There are 2084876 free Kbytes of virtual memory.
// There are 0 free Kbytes of extended memory.
#include <windows.h>
#include <stdio.h>
// Use to convert bytes to KB
#define DIV 1024
// Specify the width of the field in which to print the numbers.
// The asterisk in the format specifier "%*I64d" takes an integer
// argument and uses it to pad and right justify the number.
#define WIDTH 7
void main(int argc, char *argv[])
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
printf ("There is %*ld percent of memory in use./n",
WIDTH, statex.dwMemoryLoad);
printf ("There are %*I64d total Kbytes of physical memory./n",
WIDTH, statex.ullTotalPhys/DIV);
printf ("There are %*I64d free Kbytes of physical memory./n",
WIDTH, statex.ullAvailPhys/DIV);
printf ("There are %*I64d total Kbytes of paging file./n",
WIDTH, statex.ullTotalPageFile/DIV);
printf ("There are %*I64d free Kbytes of paging file./n",
WIDTH, statex.ullAvailPageFile/DIV);
printf ("There are %*I64d total Kbytes of virtual memory./n",
WIDTH, statex.ullTotalVirtual/DIV);
printf ("There are %*I64d free Kbytes of virtual memory./n",
WIDTH, statex.ullAvailVirtual/DIV);
// Show the amount of extended memory available.
printf ("There are %*I64d free Kbytes of extended memory./n",
WIDTH, statex.ullAvailExtendedVirtual/DIV);
}
MSDN中获取特定程序内存使用情况的例子:
Collecting Memory Usage Information For a Process
To determine the efficiency of your application, you may want to examine its memory usage. The following sample code uses the GetProcessMemoryInfo function to obtain information about the memory usage of a process.
Copy Code
The main function obtains a list of processes by using the EnumProcesses function. For each process, main calls the PrintMemoryInfo function, passing the process identifier. PrintMemoryInfo in turn calls the OpenProcess function to obtain the process handle. If OpenProcess fails, the output shows only the process identifier. For example, OpenProcess fails for the Idle and CSRSS processes because their access restrictions prevent user-level code from opening them. Finally, PrintMemoryInfo calls the GetProcessMemoryInfo function to obtain the memory usage information.
2008年06月05日 星期四 16:58
得到cpu占有率的API函数: GetSystemTimes 得到内存使用情况的API函数: GlobalMemoryStatusEx Function Retrieves information about the system's current usage of both physical and virtual memory. GetPerformanceInfo Function Retrieves the performance values contained in the PERFORMANCE_INFORMATION structure 获取特定程序的内存使用情况用: GetProcessMemoryInfo Function Retrieves information about the memory usage of the specified process. 我写的一个cpu使用率例子: |
#define _WIN32_WINNT 0x0501
#include <Windows.h>
#include <iostream>
using namespace std;
__int64 CompareFileTime ( FILETIME time1, FILETIME time2 )
{
__int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime ;
__int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime ;
return (b - a);
}
void main()
{
HANDLE hEvent;
BOOL res ;
FILETIME preidleTime;
FILETIME prekernelTime;
FILETIME preuserTime;
FILETIME idleTime;
FILETIME kernelTime;
FILETIME userTime;
res = GetSystemTimes( &idleTime, &kernelTime, &userTime );
preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime ;
hEvent = CreateEvent (NULL,FALSE,FALSE,NULL); // 初始值为 nonsignaled ,并且每次触发后自动设置为nonsignaled
while (1){
WaitForSingleObject( hEvent,1000 ); //等待500毫秒
res = GetSystemTimes( &idleTime, &kernelTime, &userTime );
int idle = CompareFileTime( preidleTime,idleTime);
int kernel = CompareFileTime( prekernelTime, kernelTime);
int user = CompareFileTime(preuserTime, userTime);
int cpu = (kernel +user - idle) *100/(kernel+user);
int cpuidle = ( idle) *100/(kernel+user);
cout << "CPU利用率:" << cpu << "%" << " CPU空闲率:" <<cpuidle << "%" <<endl;
preidleTime = idleTime;
prekernelTime = kernelTime;
preuserTime = userTime ;
}
}
运行效果如图:
MSDN中 获取内存使用情况的例子:
Example Code [C++]
The following code shows a simple use of the GlobalMemoryStatusEx function.// Sample output:
// There is 51 percent of memory in use.
// There are 2029968 total Kbytes of physical memory.
// There are 987388 free Kbytes of physical memory.
// There are 3884620 total Kbytes of paging file.
// There are 2799776 free Kbytes of paging file.
// There are 2097024 total Kbytes of virtual memory.
// There are 2084876 free Kbytes of virtual memory.
// There are 0 free Kbytes of extended memory.
#include <windows.h>
#include <stdio.h>
// Use to convert bytes to KB
#define DIV 1024
// Specify the width of the field in which to print the numbers.
// The asterisk in the format specifier "%*I64d" takes an integer
// argument and uses it to pad and right justify the number.
#define WIDTH 7
void main(int argc, char *argv[])
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
printf ("There is %*ld percent of memory in use./n",
WIDTH, statex.dwMemoryLoad);
printf ("There are %*I64d total Kbytes of physical memory./n",
WIDTH, statex.ullTotalPhys/DIV);
printf ("There are %*I64d free Kbytes of physical memory./n",
WIDTH, statex.ullAvailPhys/DIV);
printf ("There are %*I64d total Kbytes of paging file./n",
WIDTH, statex.ullTotalPageFile/DIV);
printf ("There are %*I64d free Kbytes of paging file./n",
WIDTH, statex.ullAvailPageFile/DIV);
printf ("There are %*I64d total Kbytes of virtual memory./n",
WIDTH, statex.ullTotalVirtual/DIV);
printf ("There are %*I64d free Kbytes of virtual memory./n",
WIDTH, statex.ullAvailVirtual/DIV);
// Show the amount of extended memory available.
printf ("There are %*I64d free Kbytes of extended memory./n",
WIDTH, statex.ullAvailExtendedVirtual/DIV);
}
MSDN中获取特定程序内存使用情况的例子:
Collecting Memory Usage Information For a Process
To determine the efficiency of your application, you may want to examine its memory usage. The following sample code uses the GetProcessMemoryInfo function to obtain information about the memory usage of a process.
Copy Code
#include <windows.h> #include <stdio.h> #include <psapi.h> void PrintMemoryInfo( DWORD processID ) { HANDLE hProcess; PROCESS_MEMORY_COUNTERS pmc; // Print the process identifier. printf( "/nProcess ID: %u/n", processID ); // Print information about the memory usage of the process. hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID ); if (NULL == hProcess) return; if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) ) { printf( "/tPageFaultCount: 0x%08X/n", pmc.PageFaultCount ); printf( "/tPeakWorkingSetSize: 0x%08X/n", pmc.PeakWorkingSetSize ); printf( "/tWorkingSetSize: 0x%08X/n", pmc.WorkingSetSize ); printf( "/tQuotaPeakPagedPoolUsage: 0x%08X/n", pmc.QuotaPeakPagedPoolUsage ); printf( "/tQuotaPagedPoolUsage: 0x%08X/n", pmc.QuotaPagedPoolUsage ); printf( "/tQuotaPeakNonPagedPoolUsage: 0x%08X/n", pmc.QuotaPeakNonPagedPoolUsage ); printf( "/tQuotaNonPagedPoolUsage: 0x%08X/n", pmc.QuotaNonPagedPoolUsage ); printf( "/tPagefileUsage: 0x%08X/n", pmc.PagefileUsage ); printf( "/tPeakPagefileUsage: 0x%08X/n", pmc.PeakPagefileUsage ); } CloseHandle( hProcess ); } int main( ) { // Get the list of process identifiers. DWORD aProcesses[1024], cbNeeded, cProcesses; unsigned int i; if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) return 1; // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); // Print the memory usage for each process for ( i = 0; i < cProcesses; i++ ) PrintMemoryInfo( aProcesses[i] ); return 0; }
The main function obtains a list of processes by using the EnumProcesses function. For each process, main calls the PrintMemoryInfo function, passing the process identifier. PrintMemoryInfo in turn calls the OpenProcess function to obtain the process handle. If OpenProcess fails, the output shows only the process identifier. For example, OpenProcess fails for the Idle and CSRSS processes because their access restrictions prevent user-level code from opening them. Finally, PrintMemoryInfo calls the GetProcessMemoryInfo function to obtain the memory usage information.
相关文章推荐
- 【转】编程获取系统当前cpu使用率/空闲率 、内存使用率、剩余可用内存等
- 编程获取系统当前cpu使用率/空闲率 、内存使用率、剩余可用内存等
- 编程获取系统当前cpu使用率/空闲率 、内存使用率、剩余可用内存 Nvidia GPU的利用率等
- 编程获取系统当前cpu使用率/空闲率 、内存使用率、剩余可用内存 Nvidia GPU的利用率等
- 获取手机系统大小、可用空间大小,设备可用内存及当前应用所占内存等
- 获取系统的CPU使用率、内存使用率
- 获取ios app程序当前可用的剩余内存
- android中的HandlerThread类 Runtime.getRuntime()报错null environmentAndroid执行shell命令Android获取系统剩余可用内存信息
- 获取当前系统的可用内存信息
- 获取系统剩余可用内存信息
- Unity3D 程序获取当前系统剩余内存
- 获取手机系统大小、可用空间大小,设备可用内存及当前应用所占内存等
- android获取系统剩余可用内存信息
- 安卓编程之获取系统当前时间
- python获取centos系统剩余内存方法总结
- Python获取CPU使用率、内存使用率、网络使用状态
- Qt linux获取cpu使用率、内存、网络收发速度、磁盘读写速度、磁盘剩余空间等
- C++ 获取CPU使用率、内存使用率、磁盘空间等
- QT编程QML获取当前系统时间
- Python获取CPU使用率、内存使用率、网络使用状态