The heap fragmentation problem. Releasing a contiguous block of memory in a language such as ObjC / C / C ++ is fast. Redistributing fragmented memory in comparison is slow. It is difficult to verify, because the distribution and removal pattern often affects the amount of fragmentation that you see. An example may help.
Many distributors use adjacent lists of mixed-sized linked lists. When you allocate a million of them in a loop, you are likely to have many contiguous pieces of memory that take advantage of caches, etc. when freed up in a loop, etc. That is, they look fast. When you test it during the production process, it is not so fast.
The only way to avoid this is to avoid heap fragmentation, which means ...
- Select the blocks in which N objects are stored and free each block at a time.
- It is ideal to allocate one contiguous memory area for the entire set of N objects and free it in one call.
source share