Sameer Mahajan
Sameer Mahajan

Reputation: 598

what is the real memory usage of pthread_create

I wrote a simple program of

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

void *thread(void *arg) {
  printf("thread() entered with argument '%s'\n", arg);
  pthread_exit((void*)"success");
}

main() {
  pthread_t thid;
  void *ret;

  if (pthread_create(&thid, NULL, thread, (void*)"thread 1") != 0) {
    perror("pthread_create() error");
    exit(1);
  }

  if (pthread_join(thid, &ret) != 0) {
    perror("pthread_create() error");
    exit(3);
  }

  printf("thread exited with '%s'\n", ret);
}

and compiled it into an exacutable called 'thread' on my Ubuntu 24.04. When I run it as

valgrind --tool=massif ./thread

and generate the report I get something like

->04.78% (272B) 0x40145AB: _dl_allocate_tls (in /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2)
| ->04.78% (272B) 0x48F4606: pthread_create (in /usr/lib/x86_64-linux-gnu/libc.so.6)
|   ->04.78% (272B) 0x10927F: main (in /home/sameer/profile/thread)

which indicates the memory usage of pthread_create to be only 272 bytes. However when I run it as:

valgrind --tool=massif --pages-as-heap=yes ./thread

The report shows

->05.70% (8,392,704B) 0x48E3526: pthread_create (in /usr/lib/x86_64-linux-gnu/libc.so.6)
|   ->05.70% (8,392,704B) 0x10927F: main (in /home/sameer/profile/thread)

which indicates the memory usage to be around 8MB. What is the real memory usage of pthread_create on a system? I am interested in knowing the total memory usage including system overheads etc. valgrind documentation states that simple valgrind (earlier command) may not include memory like mmap etc. It also states that pages-as-heap=yes will show memory usage including system overheads. Does it mean that I can safely assume that 8MB is the system overhead per thread on a typical Ubuntu system?

Upvotes: 0

Views: 48

Answers (1)

Paul Floyd
Paul Floyd

Reputation: 6906

It all depends on how the pthread stack is allocated. I haven't looked at the source. If it is allocated using mmap() (most likely) then Massif won't see it in normal operation since it is only looking for calls to malloc(), operator new etc.

When you specify --pages-as-heap=yes then it will be measuring based on mmap() and so it should see all allocations at the lowest possible level outside of the kernel.

You can change the pthread stack size with pthread_attr(). If you aren't doing so the size depends on RLIMIT_STACK. If that is unlimited then your stack size will probably be 2Mb.

Upvotes: 1

Related Questions