You should NOT use default memory allocator
Memory allocator is often overlooked component of a program. Very few people talk about it. You normally hear about it only when things go really wrong. However, it is one of the most critical ones. Here I will not tell you which memory allocator you should or shouldn’t use. Most of the “standard” allocators (e.g. libc, tcmalloc, jemalloc,…) will do the job just fine. My advice is use a memory allocator that helps you deliver on your goals. I personally like jemalloc for two reasons:
it has really really good profiling capabilities. This makes it easy to find out if there is a problem and where is it.
it is used by Meta, the company that possibly has the most users in the world. If it works for Meta, it probably works for me.
Here’s an example of what you can get from jemalloc profiler:
This is so called “icicle graph”. It can be very useful when trying to understand which parts of the program used memory. Sometimes it will uncover very surprising facts about your program.
One of the “killer” features of jemalloc is being able to start it in profile mode but deactivated. Profiling can then be “turned on”/activated later when needed (e.g. via an API call). This is something that can be very handy in a number of real-world use cases.
I understand that glibc allocator can be profiled as well but in my experience it’s capabilities are inferior to jemalloc. I don’t have experience with tcmalloc but I expect profiling capabilities to be good as well given that it is used by Google (fact check this :D ).
In one of my future posts I may talk a little bit more about memory profiling. For now, keep in mind that when building software you should pick memory allocator wisely. There will come a day when you will be grateful to your past self :)