【问题标题】:Is getrusage not working for me? Why?getrusage 不适合我吗?为什么?
【发布时间】:2019-07-31 14:14:16
【问题描述】:

我正在尝试测量最大驻留集大小,我发现您可以使用 getrusage 做到这一点:

当我运行它时

#include <iostream>
#include<vector>
#include <sys/resource.h>

using namespace std;

int main(int argc, char* argv[]){

    int who = RUSAGE_SELF;
    struct rusage usage;
    int ret = -1;

    vector<int> v(1024);

    ret = getrusage(who, &usage);
    if (ret == 0) cout << usage.ru_maxrss << endl;

    return 0;
}

我得到的值与我注释向量声明时的值相同。

是不是我做错了什么?

谢谢!

【问题讨论】:

  • 您的编译器是否可能优化了向量,因为它没有被使用?尝试迭代向量并将值推入 v。
  • 我知道这可以让您将资源使用率提高到某个点,所以理论上我可以在那之后使用向量,这应该考虑向量。但是,是的,我尝试在向量上写入值。无论如何感谢您的评论。
  • 您是否尝试过将 1024 修改为更大的值(例如 100,000)?也许 1024 不足以导致内存增加。
  • 为什么还不够?理论上它是 KB 的数量,所以 1024 个整数的向量应该增加 4KB。无论如何,我尝试了 1024000 并且输出的增加对我来说没有任何意义:/ 感谢您的评论。

标签: c++ linux getrusage


【解决方案1】:

我对你的代码进行了一些修改,以便你理解 rusage 结构的 ru_maxrss 元素显示的数据。

这是我的代码

#include <vector>
#include <sys/resource.h>

using namespace std;

int main(int argc, char* argv[]){

    int who = RUSAGE_SELF;
    struct rusage usage;
    int ret = -1;
    long int a,b;

getrusage(who, &usage);
a=usage.ru_maxrss;

cout << "Maximum resident set size before vector allocation   "<<a << endl;
    vector<int> v(10240000);

cout<<"Size of vector v in kilobytes (kB)  "<<(v.size()*sizeof(int))/(1024)<<endl;

    ret = getrusage(who, &usage);

    cout<<"Size of integer data type   "<<sizeof(int)<<endl;
    b=usage.ru_maxrss;

    if (ret == 0) 
cout << "Maximum resident set size after vector allocation  "<<b << endl;
 cout<<"Difference between resident set size before and after vector allocation  "<<b-a<<endl;

    return 0;
}

每次调用 getrusage() 时,它都会分配一个不同的驻留集大小,该大小比分配给向量的大小大几 kB(大约 600-800)。

上述代码的一些输出如下

如果我减少编号。向量中的元素到 1024000 输出如下

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-03
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    相关资源
    最近更新 更多