2
votes

we experience a high memory overhead when reusing threads that use Intel TBB functions. We expected that once a thread has finished a given workload, it would free the respective memory. However, this does not seem to be the case, even if there are long pauses between the execution of work units by a thread.

We prepared an example to show the problem:

int main() {
   blocking_queue<size_t> command_input_queue;
   tbb::atomic<size_t> count = 1;
   //workers
   std::vector<std::thread> worker;
   for(size_t i = 0; i < 15; i++) {
      worker.push_back(std::thread([&command_input_queue, &count](){
        while(true)
        {
            size_t size;
            //wait for work..
            command_input_queue.wait_and_pop(size);
            //do some work with Intel TBB
            std::vector<int32_t> result(size);
            for(size_t i = 0; i < result.size(); i++) {
                result[i] =  i % 1000;
            }
            tbb::parallel_sort(result.begin(), result.end());
            size_t local_count = count++;
            std::cout << local_count << " work items executed " << std::endl;
        }
    }));
   }
   //enqueue work
   size_t work_items = 15;
   for(size_t i = 0; i < work_items   ; i++) {
      command_input_queue.push(10 * 1000 * 1000);
   }

   while(true) {
      boost::this_thread::sleep( boost::posix_time::seconds(1) );
      if(count > 15) {
         break;
      }
   }
   //wait for more commands
   std::cout << "Wait" << std::endl;
   boost::this_thread::sleep( boost::posix_time::seconds(60) );

   //----!During the wait, while no thread is active, 
   //the process still claims over 500 MB of memory!----
   for(size_t i = 0; i < 15; i++) {
     command_input_queue.push(1000 * 1000);
   }
...

In the example we start 15 worker threads. They wait for tasks and do a tbb::parallel_sort and release all resources after they are done. The problem is after all tasks are processed and all workers wait for new tasks, the process still claims 500MB of memory.

Tools like valgrind's massif didn't show us where the memory is claimed. We linked the program against the libtbb.so. So tbb allocators shouldn't be the problem.

Does anybody know how we can release the memory, while a worker is idle?

2
I took your test, modified it a bit (replaced blocking_queue with tbb::concurrent_bounded_queue, and boost::this_thread_sleep with std::this_thread::sleep_for), and compiled with Visual Studio 2015 and Intel TBB 2017 Update 2. The resulting test uses ~500MB at peak, but quickly goes down to ~1.7MB when the main thread starts waiting - i.e. the problem was not reproduced. - Alexey Kukanov
Thanks for taking the effort to try to reproduce the issue. I did the test on Ubuntu 14.04 also with TBB 2017. On my system the memory consumption doesn't go done, even if I run your modified test. But after calling malloc_trim (See answer below) it goes down to ~2MB. So it seems to be system dependent. - Robert Seilbeck

2 Answers

3
votes

Heap-allocated memory is not normally returned to the OS after a call to delete or free. You need to call malloc_trim or your allocator-specific function to do that.

2
votes

TBB scheduler caches task allocations despite of connected allocator, though it does not explain 500MB. What might explain is that TBB dynamically loads TBB allocator, which caches memory of course, if it can find it next to libtbb.so. You can check whether the tbbmalloc is activated by setting env var TBB_VERSION=1

What looks strange to me is why do you oversubscribe machine with your worker threads while TBB creates its own workers?