https://blog.csdn.net/imred/article/details/50643022
getrusage - get resource usage,该函数可以程序统计对系统资源的使用量。(实际上是统计了进程执行直到调用该函数时的资源用量,如果在不同的时间调用该函数,会得到不同的结果)
#include <unistd.h> #include <sys/resource.h> void* MemoryTraceFunc(void*) { #ifndef WIN32 while (true) { rusage usage; getrusage(RUSAGE_SELF, &usage); int ru_maxrss = static_cast<int>(usage.ru_maxrss / 1024 ); //MB std::cout << "MemUse now: " << ru_maxrss << " MB." << std::endl; usleep(1000000); } #else while (true) { PROCESS_MEMORY_COUNTERS info; GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) ); nimbus::DataVar::procise_peak_used_memory_ = info.PeakWorkingSetSize / 1024 / 1024; nimbus::DataVar::current_used_memory_ = info.WorkingSetSize / 1024 / 1024; if (! nimbus::DataVar::cmd_started_) nimbus::DataVar::cmd_peak_used_memory_ = nimbus::DataVar::current_used_memory_; else if (nimbus::DataVar::cmd_peak_used_memory_ < nimbus::DataVar::current_used_memory_) nimbus::DataVar::cmd_peak_used_memory_ = nimbus::DataVar::current_used_memory_; Sleep(1); } #endif return NULL; }