AR# 53694

PetaLinux - Memory leak when assigning memory less than 64 bytes


For PetaLinux versions 3.1, 12.09 and 12.12, there is a memory leak if allocating a chunk of memory less than 64 bytes.


Memory is allocated correctly, but a subsequent free(p) on the pointer to memory returned from malloc does not add the chunk to the fastbin list, meaning future mallocs less than 64 bytes will be unable to utilize this chunk, and will allocate new memory instead.
Memory allocation / freeing of chunks 64 bytes and larger are unaffected.
This bug occurs in the MicroBlaze tool chain, and will be addressed in the next PetaLinux release.
A workaround is to disable the use of fastbins for small memory chunks, by calling;
mallopt( M_MXFAST, 0 );
BEFORE the malloc call.
Alternatively,users can lower the M_MMAP_THRESHOLD to 0, which will tune malloc to use the mmap syscall for all memory allocations (normally used only for allocating larger chunks >= ~128Kbytes), again bypassing the usage of fastbin by setting the threshold to 0 bytes via mallopt;

mallopt( M_MMAP_THRESHOLD, 0 );

or by exporting environment variable;


(Note the trailing '_')
The use of mmap for all memory allocations though has the disadvantage of being a more expensive task for the kernel than brk, and not being able to reuse deallocated space in future allocations. However, this approach has the advantage that it can be enabled without modifying the application program, instead simply setting an environment variable.
AR# 53694
日期 01/21/2013
状态 Active
Type 综合文章
Tools More Less