gpt4 book ai didi

c - 子线程中的 malloc 占用太多虚拟内存

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:23:08 25 4
gpt4 key购买 nike

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);
}

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

int main()
{
pthread_t pid;
pthread_create(&pid, NULL, thread_client_timeout_check, NULL);
pause();
}
  1. pthread_create之前,top virt0.3m
  2. pthread_create后,top virt8.3m(pthread栈大小为8m)
  3. malloc(1)之后,top virt72.3m

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

最佳答案

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

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

关于c - 子线程中的 malloc 占用太多虚拟内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28380440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com