Very confused about malloc in Linux, Help!!!

Shigang Zan zanshigang@hotmail.com
Mon Oct 11 15:33:00 GMT 2010


> 2010/10/11 Shigang Zan :
>> 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.
>
> Why do you want to know?
>
> If you are trying to educate yourself, I suggest reading the code.
> There are many different heuristics involved in deciding when to
> create an arena, we can't answer them in general here.
>
> If you are trying to answer *another* question like "How do I reduce
> total virtual memory consumption?" then the answer is going to be
> "Write your own malloc." The glibc malloc implementation will allocate
> large arenas and if you have threads it will try to use one arena per
> thread to increase allocation concurrency.
>
> The malloc in glibc is based on ptmalloc2, which is based on dlmalloc.
>
> This malloc aims to be the fastest while also being among the most
> space-conserving (not space saving), portable and tunable malloc.

Thanks so much for the explanation.
Now I have met a problem, more and more blocks are created. And system memory is exhausted, program crash.

Does one block be created only for one thread or for many threads?

I have checked no memory leak found using valgrind. I don't know how to debug or fix this issue.
 
 
Another question, to improve malloc performace, what size is best to request memory. 
 
For example, 64 bytes or 62 bytes or 68 bytes if I want to use the buffer to put one 60 bytes string.

> Cheers,
> Carlos. 		 	   		  



More information about the Libc-help mailing list