Very confused about malloc in Linux, Help!!!

Shigang Zan zanshigang@hotmail.com
Mon Oct 11 14:56:00 GMT 2010


> 2010/10/9 Shigang Zan :
>> So my question is:
>>
>> 1. What malloc did do to cause scenario 2? Both 1 and 2 are multi-thread program.
>
> Malloc may be using already allocated arenas to allocate your blocks.
> This shouldn't be a problem and will not effect the performance of
> your program.

Then how does malloc know when to create one areana or not? In my system, I feel, program created more and more blocks. What is reason to create them. Normal heap only has 40M, why doesn't extend it but create more high address block. I want to know the behavior of malloc, I found the blocks won't be freed from /proc/*/smaps.

>> 2. Can malloc free memory well in scenario 2?
>
> Yes.
>
> If you have a problem you need to provide a test case.
>
> Malloc may keep an arena around like a cache to avoid future mmaps.
>
> Please keep in mind that POSIX says free will ensure that space is
> "made available for further allocation," but it doesn't say it is
> given back to the OS.
>
> You have to allow the OSs VM to handle things.
>
> If you need more explicit control, write your own malloc.
>
> Cheers,
> Carlos. 		 	   		  



More information about the Libc-help mailing list