std :: deque

时间:2015-04-24 14:46:05

标签: c++ memory heap-memory deque massif

std::deque的内存使用情况
我试图找到deque的内存使用情况。
写在简单的C ++程序下面,创建一个deque容器并推送int元素的数量。
I想要遍历容器至少一次,所以找到算法。
我的主要重点是在元素数量增加时理解内存使用和内存使用量的增长。

我尝试使用两种不同的工具valgrind massif和/ usr / bin / time进行分析 使用命令行参数1,2,3,4和下面记录的内存来运行程序

Number of Elements, heap by massif, Peak RSS (resident segment size) by top or time
1024, 4752 Bytes,   4128 Kilo Bytes
2K,   9008 Bytes,   4160 Kilo Bytes
3k,   13104 Bytes,  4176 Kilo Bytes
4k,   17520 Bytes,  4208 Kilo Bytes

根据valgrind,对于每1K元素,大约4K字节增加。但top或/ usr / bin / time记录表明,对于每1K个元素,增加了16千字节或32千字节的RSS。 STL容器元素是默认在堆上分配的,所以我希望valgrind massif报告的堆大小增长应该与top报告的RSS增长或/ usr / bin / time增加时的元素数相匹配。当4 KB的唯一额外堆增加时,为什么RSS增长了16千字节或32千字节?是什么原因导致除了堆分配之外的RSS增加?这里堆栈是相同的,因为相同的程序使用不同的命令行参数1,2,3,4运行。

#include <iostream>
#include <deque>
#include <cstdlib>
#include <cstdint>
#include <ctime>
#include <algorithm>
#include <sys/time.h>

int main ( int argc, char *argv[] )
{
    std::deque<uint32_t>  int_deque;
    int element_count = 1024*10;
    if (argc > 1)
    {
        element_count = 1024*atoi(argv[1]);
    }
    --element_count;
    std::srand(std::time(0));

    for (int i=0; i < element_count; ++i)
    {
        int_deque.push_back(rand());
    }

    uint32_t num2 = rand();
    int_deque.push_back(num2);

    if (std::find(int_deque.begin(), int_deque.end(), num2) == int_deque.end())
    {
        std::cerr << "deque error" << std::endl;
        return 1;
    }
    return EXIT_SUCCESS;
} 

0 个答案:

没有答案
相关问题