Linux 多线程内存占用分析

在一次偶然的应程序开发过程中,发现一个很奇怪的问题。大概现象为,一个很简单的程序逻辑,开启了几个线程,程序本身并没有过多的申请内存,或者说根本没有申请内存,但是在实际运行起来后,通过PS命令和status 查看内存占用居然达到了40M,详细分析了smaps文件没有得到实际的分析结果。突然想到有可能是线程导致了进程的内存增加,于是便开始了测试代码的编写,测试代码如下:

/* test_thread.c */
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
/* 测试第一个线程 */
void * test_thread(void * pParam)
{
    while(1)
    {
        sleep(1);
    }
    return (void*)0;
}
int main(int argc , char ** argv)
{
    /* 启动一个线程 */
    pthread_t thread_id = 0;
    pthread_create(&thread_id,0,test_thread,0);
    while(1)
    {
        sleep(1);
    }
    return 0;
}
[oracle@localhost test_thread]$ gcc test_thread.c -lpthread
[oracle@localhost test_thread]$ ls
a.out  test_thread.c
[oracle@localhost test_thread]$ ./a.out

一个简单的多线程测试程序,该进程开启了两个线程,一个主线程,一个子线程。通过:

[oracle@localhost test_thread]$ ps -aux | grep a.out
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
oracle   22725  0.0  0.0  12320   428 pts/0    Sl+  21:47   0:00 ./a.out
oracle   22744  0.0  0.0   5952   748 pts/1    S+   21:47   0:00 grep a.out

得到一个简单的多线程程序居然占了10MB内存之多,于是想到了多线程的线程栈空间问题,虽然多线程在运行时是共享内存空间的,但是各个线程之间的栈区还是是相对独立的,是不是在启动线程时,操作系统默认给子线程分配了过多的内存?二话不说,开始测试:

void get_thread_stacksize()
{
    pthread_attr_t pattr;
    int status = 0;
    size_t size = 0;
    //printf("default size:%d\n", size);
    status = pthread_attr_getstacksize(&pattr, &size);
    if(0 != status)
    {
        printf("pthread_attr_getstacksize err [%d]\n",status);
        return -1;
    }
    printf("current thread stack size:%d\n", size);
    return 0;
}

输出:

[oracle@localhost test_thread]$ ./a.out 
current thread stack size:10485760

通过换算,刚好10MB,可见Linux默认情况下启动一个子线程需要分配10MB的线程栈空间,为了降低进程内存占用,就必须将该默认值修改,在Windows下该默认值一般为1MB,通过修改编译选项可以修改,在Linux中可以通过pthread_attr_getstacksize函数修改pthread_attr_t结构后,将pthread_attr_t结构传入pthread_create函数以修改线程默认栈大小。相关代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
//#include 
/* 测试第一个线程 */
void * test_thread(void * pParam)
{
    while(1)
    {
        sleep(1);
    }
    return (void*)0;
}
void get_thread_stacksize_for_attr(pthread_attr_t * p_pattr)
{
    int status = 0;
    size_t size = 0;
    //printf("default size:%d\n", size);
    status = pthread_attr_getstacksize(p_pattr, &size);
    if(0 != status)
    {
        printf("pthread_attr_getstacksize err [%d]\n",status);
        return -1;
    }
    printf("current thread stack size:%d\n", size);
    return 0;
}
void get_thread_stacksize()
{
    pthread_attr_t pattr;
    int status = 0;
    size_t size = 0;
    //printf("default size:%d\n", size);
    status = pthread_attr_getstacksize(&pattr, &size);
    if(0 != status)
    {
        printf("pthread_attr_getstacksize err [%d]\n",status);
        return -1;
    }
    printf("current thread stack size:%d\n", size);
    return 0;
}
void set_thread_stacksize(pthread_attr_t * p_pthread_attr_t,size_t size)
{
    //pthread_attr_t pattr;
    int status = 0;
    //printf("default size:%d\n", size);
    status = pthread_attr_setstacksize(p_pthread_attr_t, size);
    if(0 != status)
    {
        printf("pthread_attr_getstacksize err [%d]\n",status);
        return -1;
    }
    printf("set thread stack size:%d\n", size);
    return 0;
}
int main(int argc , char ** argv)
{
    /* 启动一个线程 */
    pthread_t thread_id = 0;
    pthread_attr_t pattr;
    pthread_attr_init(&pattr);
    /* 设置线程栈大小为1M */
    set_thread_stacksize(&pattr,1024*1024);
    //get_thread_stacksize();
    get_thread_stacksize_for_attr(&pattr);
    pthread_create(&thread_id,&pattr,test_thread,0);
    get_thread_stacksize();
    while(1)
    {
        sleep(1);
    }
    return 0;
}
[oracle@localhost test_thread]$ gcc *.c -lpthread
test_thread.c: 在函数‘get_thread_stacksize_for_attr’中:
test_thread.c:26: 警告:在无返回值的函数中,‘return’带返回值
test_thread.c:29: 警告:在无返回值的函数中,‘return’带返回值
test_thread.c: 在函数‘get_thread_stacksize’中:
test_thread.c:41: 警告:在无返回值的函数中,‘return’带返回值
test_thread.c:44: 警告:在无返回值的函数中,‘return’带返回值
test_thread.c: 在函数‘set_thread_stacksize’中:
test_thread.c:55: 警告:在无返回值的函数中,‘return’带返回值
test_thread.c:58: 警告:在无返回值的函数中,‘return’带返回值
[oracle@localhost test_thread]$ 
[oracle@localhost test_thread]$ 
[oracle@localhost test_thread]$ 
[oracle@localhost test_thread]$ 
[oracle@localhost test_thread]$ ./a.out 
set thread stack size:1048576
current thread stack size:1048576
current thread stack size:10485760


oracle    3631  0.0  0.0   5952   748 pts/1    S+   22:26   0:00 grep ./a.out
[oracle@localhost test_thread]$ ps -aux | grep ./a.out
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
oracle    3664  0.0  0.0   3108   508 pts/0    Sl+  22:27   0:00 ./a.out
oracle    3669  0.0  0.0   5952   776 pts/1    S+   22:27   0:00 grep ./a.out

通过测试,修改线程堆栈大小后有效的更改了启动线程时对内存空间的要求。

扩展:

1、  获取Linux默认线程栈大小

Ulimite –s

2、  修改Linux默认线程栈大小

Ulimite –s value


你可能感兴趣的:(C语言学习之路)