Currently working on a code I realized that it used to call malloc multiple times (around 10 million calls) and allocated small chunks (around 10 bytes) every time.
I changed the code a little and instead of calling malloc 10 million times I now call malloc 10 times allocating a large chunk of memory (10 million bytes) every time.
With this change I noticed that peak memory consumption of my code changed from ~15 GB to ~14 GB.
Why is this happening? Does malloc allocates some extra chunk with every call?
definitely. Because
malloc()allocates some amount of bytes for meta-data. so if multiple malloc() leads to more meta-data.This link gives you more detail about how malloc allocates memory as well as metadata.