简体   繁体   English

是否存在不会隐式释放内存的STL分配器?

[英]Is there an STL Allocator that will not implicitly free memory?

Memory usage in my STL containers is projected to be volatile - that is to say it will frequently shrink and grow. 我的STL容器中的内存使用量预计会不稳定 - 也就是说它会经常收缩和增长。 I'm thinking to account for this by specifying an allocator to the STL container type declarations. 我想通过为STL容器类型声明指定一个分配器来解决这个问题。 I understand that pool allocators are meant to handle this type of situation, but my concern is that the volatility will be more than the pool accounts for, and to overcome it I would have to do a lot of testing to determine good pool metrics. 我知道池分配器是为了处理这种情况,但我担心的是波动率将超过池的帐户,为了克服它,我必须进行大量测试以确定良好的池指标。

My ideal allocator would never implicitly release memory , and in fact is perfectly acceptable if memory is only ever released upon destruction of the allocator. 我的理想分配器永远不会隐式释放内存 ,事实上,如果内存只是在分配器被破坏后才被释放,那么它是完全可以接受的。 A member function to explicitly release unused memory would be nice, but not necessary. 显式释放未使用的内存的成员函数会很好,但不是必需的。 I know that what I'm referring to sounds like a per-object allocator and this violates the standard. 我知道我所指的听起来像是一个每个对象的分配器,这违反了标准。 I'd rather stick with the standard, but will abandon it if I can't resolve this within it. 我宁愿坚持标准,但如果我不能在其中解决这个问题,我会放弃它。

I am less concerned with initial performance and more with average performance. 我不太关心初始性能,而是更关注平均性能。 Put another way, it matters less whether a single element or a pool of them is allocated at a time, and more whether said allocation results in a call to new/malloc. 换句话说,一次分配单个元素或它们的池是否重要,更重要的是所述分配是否导致调用new / malloc。 I have no problem writing my own allocator, but does anyone know of a preexisting one that accomplishes this? 我编写自己的分配器没有问题,但是有没有人知道预先存在的分配器能够完成吗? If it makes a difference, this will be for contiguous memory containers (eg vector, deque), although a generalized solution would be nice. 如果它有所不同,这将是连续的内存容器(例如vector,deque),尽管通用的解决方案会很好。

I hope this isn't too basic. 我希望这不是太基础。

Memory will be allocated and freed more for adding items than removing them. 内存将被分配并释放更多用于添加项目而不是删除它们。

I believe that never "freeing" memory isn't possible unless you know the maximum number of elements allowed by your application. 我相信除非您知道应用程序允许的最大元素数,否则永远不会“释放”内存。 The CRT might try to allocate a larger block of memory in place, but how would you handle the failure cases? CRT可能会尝试分配更大的内存块,但您如何处理故障情况呢?

Explaination: 阐释:

To create a dynamically expanding vector, there will be a larger capacity to handle most push_backs, and a reallocation to handle when this is insufficient. 要创建动态扩展向量,将有更大的容量来处理大多数push_backs,并在不足时处理重新分配。

  • During the reallocation, a new larger piece of memory is "newed" up and the elements of the old piece of memory are copied into the new one. 在重新分配期间,新的更大的内存被“新”起来,旧的内存的元素被复制到新的内存中。
  • It is important that you don't hold any iterators while to push_back elements, because the reallocation will invalidate the memory 在push_back元素中不要持有任何迭代器是很重要的,因为重新分配会使内存无效
    locations the iterator point to. 迭代器指向的位置。

  • In c++11 and TR1, you might have perfect forwarding where only the pointer to the elements need to be copied. 在c ++ 11和TR1中,您可能有完美的转发,只需要复制指向元素的指针。 This is done via a move constructor instead of a copy constructor. 这是通过移动构造函数而不是复制构造函数完成的。

However, you seem to want to avoid reallocation as much as possible. 但是,您似乎希望尽可能避免重新分配。

Using the default allocator for vector you can specify an initial capacity. 使用向量的默认分配器,您可以指定初始容量。

  • Capacity is the memory allocated and size is the number of elements. 容量是分配的内存,大小是元素的数量。

  • Memory will only be allocated at construction and if the size reaches capacity. 内存仅在构造时分配,如果大小达到容量。 This should only happen with a push_back(); 这应该只发生在push_back();

  • The default allocator will increase the capacity by a multiple (eg. 1.5, 2.0) so that reallocation takes place in linear time. 默认分配器将容量增加一倍(例如1.5,2.0),以便重新分配在线性时间内进行。 So if you have a loop that pushes back data it's linear. 因此,如果你有一个循环推回数据它是线性的。 Or if you know the number of elements ahead of time you can allocate once. 或者,如果您提前知道元素的数量,则可以分配一次。

There are pool concepts you can explore. 您可以探索游泳池概念。 The idea with a pool is you are not destroying elements but deactivating them. 游泳池的想法是你不是破坏元素,而是停用它们。

If you still want to write your own allocator, this is a good article. 如果您仍想编写自己的分配器,这是一篇很好的文章。

custom allocators 自定义分配器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM