Linux 堆 - 进行大量的新建/删除操作是否正常,或者堆是否变得严重碎片化?
我不熟悉Linux堆是如何分配的。
我每秒多次调用 malloc()/free() ,并且始终具有相同的大小(大约有 10 个结构,每个结构大小固定)。除了初始化时间之外,我的内存都没有长时间分配。
对于标准堆来说,这是否被认为是糟糕的形式? (我确信有人会问“你正在使用什么堆?”-“呃。标准静态堆”..意思是我不确定。)
我应该使用空闲列表还是堆可以容忍大量相同的列表分配。我正在尝试平衡可读性和性能。
有什么工具可以帮助我测量吗?
I'm not familiar with how the Linux heap is allocated.
I'm calling malloc()/free() many many times a second, always with the same sizes (there are about 10 structs, each fixed size). Aside from init time, none of my memory remains allocated for long periods of time.
Is this considered poor form with the standard heaps? (I'm sure someone will ask 'what heap are you using?' - 'Ugh. The standard static heap' ..meaning I'm unsure.)
Should I instead use a free list or does the heap tolerate lots of the same allocations. I'm trying to balance readability with performance.
Any tools to help me measure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,除非您测量内存使用量激增的问题,否则不要考虑使用自定义分配器。这是过早优化的最糟糕形式之一。
同时,即使您确实遇到问题,比自定义分配器更好的解决方案是找出为什么要分配和释放如此多的对象,并解决导致问题的设计问题。
为了解决您的具体问题,glibc 的分配器基于 dlmalloc 算法,该算法在碎片方面接近最佳。导致严重碎片化内存的唯一方法是不可避免的方法:交替分配具有完全不同生命周期的对象,例如分配大量对象但只释放所有其他对象。我认为您将很难制定出一种分配模式,该模式会比池提供更糟糕的总内存使用量......
First of all, unless you have measured a problem with memory usage blowing up, don't even think about using a custom allocator. It's one of the worst forms of premature optimization.
At the same time, even if you do have a problem, a better solution than a custom allocator would be figuring out why you're allocating and freeing objects so much, and fixing the design issue that's causing it.
To address your specific question, glibc's allocator is based on the dlmalloc algorithm, which is near-optimal when it comes to fragmentation. The only way you'll get it to badly fragment memory is the unavoidable way: by allocating objects with radically different lifetimes in alternation, e.g. allocating a large number of objects but only freeing every other one. I think you'll have a hard time working out an allocation pattern that will give worse total memory usage than pools...
Valgrind 有一个特殊的工具 Massif 用于测量内存使用情况。这应该有助于分析堆分配。
Valgrind has a special tool Massif for measuring memory usage. This should help to profile heap allocations.
我认为最好的性能优化是尽可能(且合理)地避免堆分配。每当一个对象被堆栈分配时,编译器只会将堆栈指针向上移动,而不是尝试找到空闲位置或将分配的内存返回到某个空闲列表。
你们的结构的寿命是如何定义的?如果您可以通过范围来表达对象的生命周期,那么这确实会提高性能。
I think the best performance optimization is to avoid heap allocation wherever it is possible (and reasonable). Whenever an object is stack allocated the compiler will just move the stack pointer up instead of trying to find a free spot or return the allocated memory to some free list.
How is the lifetime of your structures defined? If you can express your object lifetime by scoping then this would indeed increase performance.