std::deque 内存使用
我已经实现了一个简单的统计引擎,使用双端队列提供数据队列来返回滚动平均值和方差。
双端队列由多个条目构成,条目数量等于滚动值的数量。
当新值到达时,最旧的值将从前面弹出,并将新值推到后面。
我需要确保这不会在内存中增长,因为它预计会作为后台任务运行很长时间。
双端队列是否在正在使用的堆上分配? 是否有可以用来固定其大小的标志?
我在 RHEL 5.3 上使用 G++ 4.1.2
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
本质上,任何动态大小的容器都会从堆中分配内存。另一个问题提供了双端队列实现的概述。
但在您的特定情况下,队列始终具有相同的大小。如果您遇到双端队列问题,使用 循环缓冲区实现简单的固定大小队列可能会有所帮助 在固定大小的数组上。此实现应该具有从根本上更好的内存行为(因为它从不需要重新分配)。如果不分析数据,很难评估其优势是否值得实施。
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.
提示一下,如果您不需要跟踪这些值,可以使用这个很棒的算法,它非常轻量级(我什至在 8 位微处理器上使用它)并且非常准确。
该算法由 BP Welford 创建,并在 Donald Knuth 的《计算机编程艺术》第 2 卷第 232 页第 3 版中介绍。
http://www.johndcook.com/standard_deviation.html
Just as a tip, if you don't need to keep track of the values there is this great algorithm that is very lightweight (I even use it on 8bit micros) and is accurate.
This algorithm was created by B. P. Welford and is presented in Donald Knuth's Art of Computer Programming, Vol 2, page 232, 3rd edition.
http://www.johndcook.com/standard_deviation.html
该规范将实施细节留给供应商。然而,由于两端的插入效率很高,因此它很可能在堆上实现为链接结构。话虽如此,当您从堆中弹出某些内容时,它应该被解构,因此您的总内存使用量不应增加。
The specification leaves implementation details to the vendor. However, since insertion at both ends is efficient, it is most likely implemented as a linked structure on the heap. That being said, when you pop something off the heap, it should get deconstructed, so your total memory usage shouldn't climb.