它是更好地两种动力分配内存? [英] Is it better to allocate memory in the power of two?

查看:117
本文介绍了它是更好地两种动力分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用的malloc()来分配内存,我们应该给它的大小是两种力量?或者,我们只是给我们所需要的确切大小?

如同

  //的char * PTR =的malloc(200);
字符* PTR =的malloc(256); //,而不是200,我们使用256

如果这是更好地给大小是两种力量,究竟是什么原因呢?为什么好?

感谢

修改

我困惑的原因是以下乔尔的博客歌姬报价


  

聪明的程序员减少
  由malloc的潜在distruption
  总分配的内存块
  对于规模2的幂。

  知道,4个字节,8字节,16字节,
  18446744073709551616字节,等等。
  原因,应该是直观的
  任何人谁用乐高积木玩,这
  最大限度地减少怪异量
  碎片,在自由的推移
  链。尽管它可能看起来像这样
  浪费空间,但也很容易看到
  它如何从不浪费的50%以上的
  空间。所以,你的程序不使用
  超过两倍的内存,因为它
  需要,这不是什么大不了的
  这笔交易。


对不起,我应该已经发布上​​述报价较早。我的歉意!

多数答复,到目前为止,说两种动力分配内存是一个坏主意,那么在这种情况下它能够更好地跟随乔尔的约点的malloc() ?他为什么这么说?是上面引述的建议现在已经过时了?

请解释。

谢谢


解决方案

只是给你所需要的确切大小。唯一的原因,功率为2的大小可能是更好是允许更快的分配和/​​或避免内存碎片。

然而,任何非平凡的的malloc 的实现,关系到自身以这种方式被向上有效地将在内部分配轮是否以及何时适合这样做。你并不需要帮助的malloc关心自己; malloc的可以做就好在它自己的。

编辑:

在回答您的软件文章乔尔的报价,乔尔在该节(这是很难不跟随你引述的段落的背景下正确识别)问题是,如果你期望频繁的的-allocate一个缓冲区,最好这样做乘法,而不是加法。这一点,事实上,正是在C ++中(即的std ::字符串的std ::矢量类等等)做的。

之所以说这是一个进步是不是因为你帮助了的malloc 通过提供方便的数字,但由于内存分配是一个的昂贵的操作和您要尽量减少你做的次数。乔尔是presenting一个时空权衡的理念的具体例子。他争辩说,在需要的内存量动态变化很多情况下,最好是浪费一些空间(通过分配高达两倍之多,因为你需要在每个扩展),以挽救的时间的这将需要在确切 N 字节的内存,每次你需要 N 更多字节的时间。

乘法器不必是两个:根据需要,你可以分配高达三倍多的空间,并在三个功率与分配结束,或分配最多至第五十七次尽可能多的空间,因为你需要并最终在中57的权力分配。你做的更多的超额分配,不经常,您将需要重新分配,但更多的内存,你会浪费。两种,其中最多使用两次需要多少内存分配的权力,恰好是一个很好的起点,直到权衡,除非你有你的需求是什么更好的主意。

他确实提到轻描淡写地说这有助于减少的自由连锁各自为政,但其原因是更多的是因为数量和分配的均匀性正在做的,而不是他们的确切大小。一方面,可以分配更多的时间和释放内存,你就越有可能是分片堆,在你分配什么尺寸不管。其次,如果您有动态使用相同的乘法大小调整算法调整多个缓冲区,那么很可能是,如果一个调整大小从32到64,从16到32另一个尺寸调整,那么第二个的再分配可以容纳身在何方第一个以前是。这不会是如果从25调整到60和一个与16另一至26的情况。

和再次,没有什么他谈论的适用,如果你打算做的分配步骤一次。

When we use malloc() to allocate memory, should we give the size which is in power of two? Or we just give the exact size that we need?
Like

//char *ptr= malloc( 200 ); 
char *ptr= malloc( 256 );//instead of 200 we use 256

If it is better to give size which is in the power of two, what is the reason for that? Why is it better?

Thanks

Edit

The reason of my confusion is following quote from Joel's blog Back to Basics

Smart programmers minimize the potential distruption of malloc by always allocating blocks of memory that are powers of 2 in size. You know, 4 bytes, 8 bytes, 16 bytes, 18446744073709551616 bytes, etc. For reasons that should be intuitive to anyone who plays with Lego, this minimizes the amount of weird fragmentation that goes on in the free chain. Although it may seem like this wastes space, it is also easy to see how it never wastes more than 50% of the space. So your program uses no more than twice as much memory as it needs to, which is not that big a deal.

Sorry, I should have posted the above quote earlier. My apologies!

Most replies, so far, say that allocating memory in the power of two is a bad idea, then in which scenario its better to follow Joel's point about malloc()? Why did he say that? Is the above quoted suggestion obsolete now?

Kindly explain it.
Thanks

解决方案

Just give the exact size you need. The only reason that a power-of-two size might be "better" is to allow quicker allocation and/or to avoid memory fragmentation.

However, any non-trivial malloc implementation that concerns itself with being efficient will internally round allocations up in this way if and when it is appropriate to do so. You don't need to concern yourself with "helping" malloc; malloc can do just fine on its own.

Edit:

In response to your quote of the Joel on Software article, Joel's point in that section (which is hard to correctly discern without the context that follows the paragraph that you quoted) is that if you are expecting to frequently re-allocate a buffer, it's better to do so multiplicatively, rather than additively. This is, in fact, exactly what the std::string and std::vector classes in C++ (among others) do.

The reason that this is an improvement is not because you are helping out malloc by providing convenient numbers, but because memory allocation is an expensive operation, and you are trying to minimize the number of times you do it. Joel is presenting a concrete example of the idea of a time-space tradeoff. He's arguing that, in many cases where the amount of memory needed changes dynamically, it's better to waste some space (by allocating up to twice as much as you need at each expansion) in order to save the time that would be required to repeatedly tack on exactly n bytes of memory, every time you need n more bytes.

The multiplier doesn't have to be two: you could allocate up to three times as much space as you need and end up with allocations in powers of three, or allocate up to fifty-seven times as much space as you need and end up with allocations in powers of fifty-seven. The more over-allocation you do, the less frequently you will need to re-allocate, but the more memory you will waste. Allocating in powers of two, which uses at most twice as much memory as needed, just happens to be a good starting-point tradeoff until and unless you have a better idea of exactly what your needs are.

He does mention in passing that this helps reduce "fragmentation in the free chain", but the reason for that is more because of the number and uniformity of allocations being done, rather than their exact size. For one thing, the more times you allocate and deallocate memory, the more likely you are to fragment the heap, no matter in what size you're allocating. Secondly, if you have multiple buffers that you are dynamically resizing using the same multiplicative resizing algorithm, then it's likely that if one resizes from 32 to 64, and another resizes from 16 to 32, then the second's reallocation can fit right where the first one used to be. This wouldn't be the case if one resized from 25 to 60 and and the other from 16 to 26.

And again, none of what he's talking about applies if you're going to be doing the allocation step only once.

这篇关于它是更好地两种动力分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆