在C++中,是否有可能获得当前的RAM和CPU使用率?是否存在平台无关的函数调用?
遗憾的是,这些东西严重依赖底层操作系统,所以没有平台无关的调用.(也许有一些包装器框架,但我不知道.)
在Linux上,您可以查看getrusage()函数调用,在Windows上,您可以使用GetProcessMemoryInfo()进行RAM使用.还要看一下Windows 的Process Status API中的其他功能.
我知道没有与平台无关的功能.如果您计划针对多个版本的Windows,请注意某些版本的实现会有所不同.我在NT 3.51下测试应用程序时遇到了这个问题......(过时,我知道).
这是我用于内存方面的一些代码.这不适用于除Windows以外的平台,并且在没有WIN32定义的情况下编译时将返回0:
编辑:我忘了提,这个代码分为并向下舍入到最近的MB,因此>> 20到处都是.
// get memory info... int getTotalRAM() { int ret = 0; #ifdef WIN32 DWORD v = GetVersion(); DWORD major = (DWORD)(LOBYTE(LOWORD(v))); DWORD minor = (DWORD)(HIBYTE(LOWORD(v))); DWORD build; if (v < 0x80000000) build = (DWORD)(HIWORD(v)); else build = 0; // because compiler static links the function... BOOL (__stdcall*GMSEx)(LPMEMORYSTATUSEX) = 0; HINSTANCE hIL = LoadLibrary(L"kernel32.dll"); GMSEx = (BOOL(__stdcall*)(LPMEMORYSTATUSEX))GetProcAddress(hIL, "GlobalMemoryStatusEx"); if(GMSEx) { MEMORYSTATUSEX m; m.dwLength = sizeof(m); if(GMSEx(&m)) { ret = (int)(m.ullTotalPhys>>20); } } else { MEMORYSTATUS m; m.dwLength = sizeof(m); GlobalMemoryStatus(&m); ret = (int)(m.dwTotalPhys>>20); } #endif return ret; } int getAvailRAM() { int ret = 0; #ifdef WIN32 DWORD v = GetVersion(); DWORD major = (DWORD)(LOBYTE(LOWORD(v))); DWORD minor = (DWORD)(HIBYTE(LOWORD(v))); DWORD build; if (v < 0x80000000) build = (DWORD)(HIWORD(v)); else build = 0; // because compiler static links the function... BOOL (__stdcall*GMSEx)(LPMEMORYSTATUSEX) = 0; HINSTANCE hIL = LoadLibrary(L"kernel32.dll"); GMSEx = (BOOL(__stdcall*)(LPMEMORYSTATUSEX))GetProcAddress(hIL, "GlobalMemoryStatusEx"); if(GMSEx) { MEMORYSTATUSEX m; m.dwLength = sizeof(m); if(GMSEx(&m)) { ret = (int)(m.ullAvailPhys>>20); } } else { MEMORYSTATUS m; m.dwLength = sizeof(m); GlobalMemoryStatus(&m); ret = (int)(m.dwAvailPhys>>20); } #endif return ret; } int getTotalMemory() { int ret = 0; #ifdef WIN32 DWORD v = GetVersion(); DWORD major = (DWORD)(LOBYTE(LOWORD(v))); DWORD minor = (DWORD)(HIBYTE(LOWORD(v))); DWORD build; if (v < 0x80000000) build = (DWORD)(HIWORD(v)); else build = 0; // because compiler static links the function... BOOL (__stdcall*GMSEx)(LPMEMORYSTATUSEX) = 0; HINSTANCE hIL = LoadLibrary(L"kernel32.dll"); GMSEx = (BOOL(__stdcall*)(LPMEMORYSTATUSEX))GetProcAddress(hIL, "GlobalMemoryStatusEx"); if(GMSEx) { MEMORYSTATUSEX m; m.dwLength = sizeof(m); if(GMSEx(&m)) { ret = (int)(m.ullTotalPhys>>20) + (int)(m.ullTotalVirtual>>20); } } else { MEMORYSTATUS m; m.dwLength = sizeof(m); GlobalMemoryStatus(&m); ret = (int)(m.dwTotalPhys>>20) + (int)(m.dwTotalVirtual>>20); } #endif return ret; } int getAvailMemory() { int ret = 0; #ifdef WIN32 DWORD v = GetVersion(); DWORD major = (DWORD)(LOBYTE(LOWORD(v))); DWORD minor = (DWORD)(HIBYTE(LOWORD(v))); DWORD build; if (v < 0x80000000) build = (DWORD)(HIWORD(v)); else build = 0; // because compiler static links the function... BOOL (__stdcall*GMSEx)(LPMEMORYSTATUSEX) = 0; HINSTANCE hIL = LoadLibrary(L"kernel32.dll"); GMSEx = (BOOL(__stdcall*)(LPMEMORYSTATUSEX))GetProcAddress(hIL, "GlobalMemoryStatusEx"); if(GMSEx) { MEMORYSTATUSEX m; m.dwLength = sizeof(m); if(GMSEx(&m)) { ret = (int)(m.ullAvailPhys>>20) + (int)(m.ullAvailVirtual>>20); } } else { MEMORYSTATUS m; m.dwLength = sizeof(m); GlobalMemoryStatus(&m); ret = (int)(m.dwAvailPhys>>20) + (int)(m.dwAvailVirtual>>20); } #endif return ret; }
有一个开源库可以跨多个平台提供这些(以及更多系统信息):SIGAR API
我已经在相当大的项目中使用它并且它工作正常(除了OS X上的某些极端情况等)
不,没有,不在标准中.
如果您确实需要此信息,则必须编写特定于平台的#ifdefs或链接到提供它的库.