Search code examples
cpthreadsheap-memory

Is there a way to reduce the heap size of threads in C language?


massif diagram

I used Valgrind's Massif feature to check the memory usage of my C language program to investigate memory usage. I had many dynamic allocations in my code, but according to Massif, I found that it was consistently using the same amount of memory. Upon further investigation, I discovered that my program uses two threads, and each thread consistently allocates 64MB of heap memory.

According to the attached diagram, it can be observed that when two threads were initially created, they used 150.3MB of memory. When one thread was terminated, there was a precise reduction of 64MB, resulting in 86.3MB of memory usage.

I don't perform dynamic allocations that would justify the need for 64MB of heap memory. This seems like a complete waste

Does creating threads in C language inherently lead to using a fixed 64MB of heap memory? Is there a way to reduce the heap memory allocated to threads?

OS: Ubuntu 22.04

I tried adjusting the stack size, but it was naturally ineffective. I want to reduce the 64MB of heap memory allocated to threads.


Solution

  • Here is a simple example to show that it isn't the creation of the pthreads that takes up all that space. Your threads must have there own memory allocations associated with them. In that case you may need to re-engineer your program to use memory more efficiently, if that's possible.

    With this program I can easily create 10,000+ threads without even a 1GB bump in memory usage.

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <pthread.h>
    
    // gcc threadmemtest.c -o threadmemtest.bin -O3 -Wall -pthread -march=native
    
    void* sleepforabit(void*arg) {
      sleep(25);
      return NULL;
    }
    
    int main(int argc, char **argv){
      int i, numthreads = 5;
      if (argc > 1) numthreads = atoi(argv[1]);  
      pthread_t *threadid = malloc(numthreads*sizeof(pthread_t));
      for (i=0; i<numthreads; i++) {
        if (pthread_create(&threadid[i], NULL, sleepforabit, NULL) != 0) {
          fprintf(stderr, "Error creating pthread[%i].\n", i);
          numthreads = i;
          break;
        }
      }
      for (i=0; i<numthreads; i++) {
        pthread_join(threadid[i], NULL);
      }
      free(threadid);
      pthread_exit(0);
    }