Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

TBB Memory Allocator

Shankar1
Beginner
770 Views
I was looking into the memory allocators provided by TBB and I find that for
allocating a memory greater than 8K size, TBB uses malloc to allocate memory rather than
virtual alloc.I also find a preprocessor being defined by which i can change the allocation to be done by virtualalloc.But I find that the performance is way better by using malloc rather than virtualalloc.

Im totally new to memory allocation techniques and im just curious to know as to why TBB uses malloc to allocate such large sizes and as to why the performance is better this way.And also would want to know the cases where virtualalloc has to be the choice over malloc and vice versa (i.e,pros and cons of malloc over virtualalloc).
0 Kudos
3 Replies
Alexey-Kukanov
Employee
770 Views

VirtualAlloc is thesystem call that maps physical memory to the address space of the requesting process. As a system call, it is slow; and it should never be used directly in applications. An application should use memory allocation routines instead. Of those, malloc is the most known. It's the C runtime call that returns the address to a memory block of requested size. Internally, it refers to VirtualAlloc (or other system calls, e.g. mmap or sbrk, depending on the operating system); but it does it smartly, requesting memory from OS in big portions, and managing smaller memory blocks for application requests in internal structure called a heap. C++ memory allocation via new and delete operators usually refers to malloc.

The TBB memory allocator provides an alternative to malloc called scalable_malloc. So it also does memory management, but it's especially designed to use in multithreaded applications serving concurrent allocation requests faster. 8K is the approximate size limit for requests that scalable_allocator serves from its own structures; for bigger requests, it refers to malloc but not to VirtualAlloc because malloc is faster.

0 Kudos
Shankar1
Beginner
770 Views

Thank you very much...I have another question as well then..

I read in the book Intel Threading Building Blocks by James Reinders (chapter 6 Scalable Memory Allocation, page 101) that scalable memory allocator comes at a cost of virtual space and that it wastes a lot of space when allocating blocks int the 9K to 12K range.

But I see malloc being used to allocate memoryfor sizes greater than 8K bytes.Can you please clarify as to how and why this wastage of virtualspace happens?If that is really the case, then whydoesit happen only in the 9Kto 12Krange.?Why not for sizes more than 12K.?

And I would also be pleasedto knowin which way the scalable allocator is not terribly sophisticated about paging issues (as told in the book)?

0 Kudos
Alexey-Kukanov
Employee
770 Views

At the time the book was written, TBB scalable allocator used VirtualAlloc for large allocations. We changed it to malloc very close to TBB 2.0 release when it was late to make changes in the book.

0 Kudos
Reply