Memory pool in c ++

I have a very high performance C ++ library. I am thinking of writing a memory pool, so I do not need to use the global new and delete . I read. But I would like to know if this will help me in reducing performance and memory.

+4
source share
3 answers

If you have no particular reason to believe that your library will benefit from a custom allocator, this will most likely not help, if at all.

It looks like you are trying to perform non-oriented optimization. Not. First use profiling to collect performance data, and then consider optimizing if necessary.

+2
source

A memory allocator that is finely tuned to the exact memory allocation pattern of your application should outperform any shared allocator β€” given that your allocation patterns are different from the β€œgeneral” and predictable. However, reducing or detecting a memory leak is a completely different issue and should be resolved before any performance issues.

+1
source

In my experience, implementing your own memory pool does not help performance. Since in a multi-threaded environment you should use mutex to protect memory and free access to the pool, while mutex is very heavy compared to the new / delete system.

It also does not help reduce memory leak. To reduce memory leak, you must free all allocated memory. This really needs to be resolved using your program, not the pool.

The only advantage can help using trace memory independent of a third-party tool.

0
source

All Articles