S R Bandi
S R Bandi

Reputation: 81

memory usage by std::deque

Memory usage by std::deque
I am trying to find the memory usage for deque.
Written below simple C++ program that creates a deque container and pushes the number of int elements.
I want to traverse the container at least once so find algorithm is used.
My main emphasis is to understand the memory usage and growth of memory usage when the number of elements are increased.

I have tried to analyse using two different tools valgrind massif and /usr/bin/time Ran the program with command line args 1, 2, 3, 4 and memory captured as below

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

As per the valgrind, for every 1K elements, roughly 4K bytes is increased. But top or /usr/bin/time recordings shows that for every 1K elements, 16 kilo bytes or 32 kilo bytes of RSS is increased. STL container elements are default allocated on heap so I expect the heap size growth reported by valgrind massif should match with the RSS growth reported by top or /usr/bin/time when the number of elements are increased. Why is the RSS grown by 16kilo bytes or 32 kilo bytes when the only extra heap of 4 KB is increased? What causes the increase of RSS other than heap allocations? Here stack is same because same program is run with different command line arguments 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;
} 

Upvotes: 2

Views: 671

Answers (0)

Related Questions