Reputation: 5377
I have a multi threaded application where I allocate buffers with data, which then wait in queues to be send via sockets. All buffers are reproducible because I use only buffers of fixed size in whole program (1024, 2048, 2080 and 5248 bytes). I noticed, that my program usually use up to 10 buffers of each length type at the same moment.
So far I always manually allocate new buffer and then free it (using malloc() and free ()) where it's not needed any more. I started wondering if Linux is enough smart to cache this memory for me, so next time I allocate new buffer system only quickly receive a buffer I have already used before and not perform heavy operation of allocating new memory block?
Upvotes: 0
Views: 421
Reputation: 2197
Yes, malloc() will only call sbrk() / brk() when there is nothing in the free list that satisfies the request. This means that you can call malloc()/free() as much as you like for the same size bit of memory and it will be just fine.
Whether or not this is a really performant solution you have is another question, but it might be quick enough to not matter.
Upvotes: 2