Search code examples
c++memorystldeque

std::deque memory use


I have implemented a simple statistical engine to return rolling mean and variance using a deque to provide a data queue.

The deque is constructed with a number of entries equal to the rolling number of values.

When a new value arrives the oldest value is popped of the front and the new one pushed onto the back.

I need to be sure that this is not going to grow in memory as it is expected to run as a background task for a long time.

Does deque allocate on the heap in use? Are there flags that I can use to fix its size?

I am using G++ 4.1.2 on RHEL 5.3


Solution

  • Essentially, any dynamically sized container allocates memory from the heap. Another question provides an overview over the implementation of the deque.

    But in your particular case, the queue always has the same size. If you hit problems with the deque, it might be beneficial to implement a simple fixed-size queue using a circular buffer on a fixed-sized array. This implementation should have fundamentally better memory behaviour (since it never requires reallocation). Whether its advantage is worth the trouble of implementing is hard to assess without profiling data.