开发者

Memory allocation in Linux

开发者 https://www.devze.com 2023-02-04 23:49 出处:网络
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

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 p开发者_如何学编程rogram (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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消