博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[摘抄]Memory Allocation/Deallocation Bottleneck?(内存分配/释放瓶颈)
阅读量:4840 次
发布时间:2019-06-11

本文共 2270 字,大约阅读时间需要 7 分钟。

本篇文章摘抄于——,主要讲了C/C++堆内存分配和释放导致性能问题的原因,并给出了基本的解决方案。

C/C++堆内存分配和释放,即通过malloc/free或new/delete操作内存,主要会引起两个问题:一是内存碎片;二是堆内存操作需多线程同步,加锁(OS级别)。

============================================================================

Q:How much of a bottleneck is memory allocation/deallocation in typical real-world programs? Answers from any type of program where performance typically matters are welcome. Are decent implementations of malloc/free/garbage collection fast enough that it's only a bottleneck in a few corner cases, or would most performance-critical software benefit significantly from trying to keep the amount of memory allocations down or having a faster malloc/free/garbage collection implementation?

Note: I'm not talking about real-time stuff here. By performace-critical, I mean stuff where throughput matters, but latency doesn't necessarily.

Edit: Although I mention malloc, this question is not intended to be C/C++ specific.

 

A1:It's significant, especially as fragmentation grows and the allocator has to hunt harder across larger heaps for the contiguous regions you request. Most performance-sensitive applications typically write their own fixed-size block allocators (eg, they ask the OS for memory 16MB at a time and then parcel it out in fixed blocks of 4kb, 16kb, etc) to avoid this issue.

In games I've seen calls to malloc()/free() consume as much as 15% of the CPU (in poorly written products), or with carefully written and optimized block allocators, as little as 5%. Given that a game has to have a consistent throughput of sixty hertz, having it stall for 500ms while a garbage collector runs occasionally isn't practical.

 

A2:Nearly every high performance application now has to use threads to exploit parallel computation. This is where the real memory allocation speed killer comes in when writing C/C++ applications.

In a C or C++ application, malloc/new must take a lock on the global heap for every operation. Even without contention locks are far from free and should be avoided as much as possible.

Java and C# are better at this because threading was designed in from the start and the memory allocators work from per-thread pools. This can be done in C/C++ as well, but it isn't automatic.

转载于:https://www.cnblogs.com/dpsincs/p/3300069.html

你可能感兴趣的文章
POJ 1611 The Suspects
查看>>
每日站立会议——Day5
查看>>
构建执法第二章读后感
查看>>
【收藏】win7打开word每次提示配置解决办法
查看>>
POJ1143 Number Game(DP)
查看>>
等价类划分例子中的些许添加
查看>>
《剑指offer》---斐波那契数列
查看>>
Vue自定义指令(directive)
查看>>
webservice使用注解修改WSDL内容
查看>>
SystemView 破解方法记录
查看>>
【vijos1642】班长的任务
查看>>
JavaScript入门基础(四)
查看>>
校内的hu测(10.5)
查看>>
Windows Forms高级界面组件-使用对话框
查看>>
Objective-C中的深拷贝和浅拷贝
查看>>
超实用的JQuery小技巧
查看>>
设计模式——单例模式 (C++实现)
查看>>
UML和模式应用学习笔记(6)——系统顺序图、系统操作和层
查看>>
Android -- startActivityForResult和setResult
查看>>
1019 General Palindromic Number (20 分)
查看>>