子线程中的 malloc 消耗了太多的虚拟内存

jia*_*sun 1 c linux multithreading glibc

void * thread_client_timeout_check(void *arg)
{
    pthread_attr_t attr;size_t size;
    pthread_attr_init(&attr);
    pthread_attr_getstacksize(&attr, &size);
    printf("pthread stacksize: %d\n", size);
    malloc(1);
}
Run Code Online (Sandbox Code Playgroud)

主线程创建子线程并暂停。

int main()
{
    pthread_t pid;
    pthread_create(&pid, NULL, thread_client_timeout_check, NULL);
    pause();
}
Run Code Online (Sandbox Code Playgroud)
  1. 之前pthread_createtop virt0.3m
  2. after pthread_create, top virtis 8.3m(pthread 堆栈大小为 8m)
  3. 之后malloc(1)top virt72.3m

为什么malloc(1)54m从内核获取虚拟内存?

jan*_*neb 5

在多线程程序中,glibc 2.10+ 创建了多个 malloc 池以减少错误共享,从而提高可扩展性。结果是,从 glibc 2.10 开始,虚拟内存使用量会高得多。但是由于地址空间很便宜,或者在 64 位架构上或多或少是免费的,所以真的没有什么可担心的。

https://udrepper.livejournal.com/20948.html