c++ - std::queue should I shrink to fit? -
i considering use std::queue
(with std::deque
) fifo structure.
in queue data structure, data pushed in , popped in front. therefore, memory in front after popping element never used.
i remember std::vector not release memory until explicitly call shrink_to_fit
method. std::deque
?
so, should consider releasing memory in front never used again?
you don't need shrink_to_fit
queue, if used normally. std::vector
's shrink_to_fit
meant situations vector content has diminished huge amount, has benefit call (rather costly) reallocation in order free huge amount of memory. in general not needed if vector has short lifetime or if size not vary much.
having said that, std::deque
different kind of beast. typically consists of fixed-size chunks of memory. if erase lots of elements deque, chunks no longer contain elements can/will deallocated. biggest memory overhead can have @ time bit under twice chunk size, e.g. if queue contains 2 elements, 1 @ end of chunk, second @ start of next chunk. therefore, std::deque::shrink_to_fit
can move elements of deque in way frees 1 chunk, wich not big gain (iirc typical implementation has chunk size of few kb).
these general statements might not apply in memory critical situations. standard containers, neither vector not deque explicitly designed used in such exetreme situations. if memory footprint issue in part of program use queue, might want use yet data structure.
Comments
Post a Comment