1
votes

We have a piece of code that utilises TBB to spawn tasks to perform some processing this is done using the following TBB code to initialise the TBB thread pool (to match the number of cores):

tbb::task_scheduler_init(8);

Then for each task we want to spawn we use the following code (where MainTask is derived from the tbb::task class):

task = new (tbb::task::allocate_root()) MainTask(theAction, theOutputData);
tbb::task::enqueue(*task);

When we run our code we start off using a certain amount of memory and then as the program executes and spawns new tasks, as described above, the memory usage gradually increases over time. In 40 minute of execution it increases from from 12KB to 213KB. This is defiantly not as a result of any of our processing code and seems to be a result of TBB not freeing up the memory from the TBB allocate_root task that we are creating once they have completed their execution as we would expect it to.

Why is this happening? Is there something else we need to do to resolve this problem or is there some alternative way to spawn the TBB tasks without using the allocate_root approach that will resolve this problem?

1

1 Answers

2
votes

Don't worry. TBB caches and reuses task memory in order to improve performance. And it has special mechanism for preventing memory leaks in producer-consumer cases where one thread keep allocating tasks and another thread only consumes them.

If you still suspect a memory leak, you need a more elaborated test which proves that memory consumption does not stop after the initial rise (of course, it is non-linear function). TBB has special unit test for detecting memory leaks: see src/test/test_task_leaks.cpp