stupid question: how to use malloc/new efficiently

David Leimbach leimy2k at mac.com
Sat Jan 17 17:08:41 CET 2004


On Jan 17, 2004, at 9:26 AM, Alexander Neundorf wrote:

> Hi,
>
> I never thought about it until I recently read it somewhere:
>
> should one always use malloc/new for buffers with sizes which are 
> multiples of
> 1K/1 page i.e. usually 4K or something like this ?
> If not, malloc will end up with a lot of small buffers of random sizes 
> which
> are hard to reuse and therefor it has to get new memory.
>
> Is this still valid or can this be ignored ?
>

This is a very platform specific question these days as different libc 
implementations
will have different malloc implementations and may in fact give you 
more memory than
you are asking for to help with fragmentation issues in actual Virtual 
Memory.

I wouldn't worry about it unless you can actually measure a performance 
problem in your
code due to this potential issue.

Optimization of these such things should be one of the last things you 
do with code not the
first unless you have a priori knowledge about your malloc 
implementation and past experience
which tells you this sort of performance degradation is caused by poor 
use of malloc/new.

Dave



More information about the Kde-optimize mailing list