Preventing memory fragmentation with the new delete method

I remember reading in a book on programming for computer games, sorry, I do not remember the name. An easy way to improve performance is something like this at the beginning:

int main() { { char dummy* = new char[10000000]; // 10Mbytes ish delete[] dummy; } ... } 

The idea is that a costly part of allocating dynamic memory is to request memory from the OS, which usually does not return until the end of the program. Has anyone used this and seen performance improvements?

+4
source share
4 answers

Whether this will work or not depends on the particular OS. Many modern operating systems use mmap under the hood for a large allocation of memory and generally bypass a lot of the process. This means that the selection will be made directly from the OS, and then will be returned directly to the OS upon release.

The strategy is much better than usual to allocate memory at the beginning and reuse space as much as possible before returning the memory to the heap. This is the logic for STL valves.

+4
source

It does not make sense. You allocate a huge block, then release it, and the heap takes responsibility for the memory occupied by the block and can legally fragment it while it is used later.

+3
source

This does not necessarily improve performance, as the current standard says nothing about how memory is dynamically allocated, freed, and redistributed. However, an implementation can use the same memory area in the rest of the program whenever it needs to allocate memory. It is more like a memory pool.

Everything could be possible. It is completely implementation dependent. Also, it can even delete the code altogether, since it does nothing.

+1
source

Depending on your environment, there may be loader flags that allow you to specify the initial heap size. This memory will be allocated when your program is loaded into memory, so it will become part of the startup time. This should give the same results, with the advantage that the compiler will not optimize it.

+1
source

Source: https://habr.com/ru/post/1340177/


All Articles