我正在尝试使用getrusage(.)和最大驻留集大小 (maxrss) 来检查内存泄漏。但是,当我故意尝试创建泄漏时,maxrss 不会改变。也许我对 maxrss 的理解不够深入。这是代码:
#include <iostream>
#include <sys/time.h>
#include <sys/resource.h>
using namespace std;
int main() {
struct rusage r_usage;
getrusage(RUSAGE_SELF, &r_usage);
cout << r_usage.ru_maxrss << "kb\n";
cout << "Allocating...\n";
int a = 100000; // have tried range of numbers
int* memleaktest = new int[a]; // class member
if(!memleaktest)
cout << "Allocation failed";
getrusage(RUSAGE_SELF, &r_usage);
cout << "after allocation " << r_usage.ru_maxrss << "kb\n";
return 0;
}
在分配(~15000kb)之后我得到了完全相同的值。在 Ubuntu x86 上。