linux 线程 虚拟内存,关于pthread多线程虚拟内存的有关问题

关于pthread多线程虚拟内存的问题

本帖最后由 jack_leiwin 于 2014-06-12 16:11:20 编辑

各位前辈,你们好

我写了一个小程序,代码如下

#include

#include

#include

using namespace std;

pthread_mutex_t fmx;

void* func(void *x)

{

int *p=(int*)x;

int **data=(int **)malloc((*p)*sizeof(int*));

for(int i=0;i

{

data[i]=(int*)malloc((*p)*sizeof(int));

for(int j=0;j

{

data[i][j]=j;

}

}

for(int i=0;i

{

for(int j=0;j

{

data[i][j]=data[i][j]*data[i][j];

}

}

for(int i=0;i

{

free(data[i]);

data[i]=0;

}

free(data);

data=0;

}

int main()

{

int k=2000;

for(int i=0;i<10;i++)

{

pthread_t threadlist[2000];

for(int j=0;j<2000;j++)

{

pthread_create(&threadlist[i],NULL,func,&k);

}

for(int j=0;j<2000;j++)

{

void *r=0;

pthread_join(threadlist[i],&r);

}

cout<

}

}

用top命令看了一下,VIRT一直在增长,增长到1百多g,这是为什么呀

103915202.gif?pthread_join()不是回收子线程内存的吗?

------解决方案--------------------

Creat的数组下表是j不是i

------解决方案--------------------

代码中没有任何的错误检查,所以很难说问题在哪。

你不能保证2000个thread都create成功了。毕竟每个新create的thread默认需要分配2M stack的,谁都无法保证2000个2M是否都能够在当前进程地址空间内分配,如果是32位程序的话光堆栈就差不多占光了地址空间了,更别说线程内那无数个malloc了。

不幸的是main函数中保存threadid的数组并未初始化(比如清零),它是有原始值的,这个原始值是程序运行过程中保存在堆栈上的无用随机数,而一旦某个pthread_create失败后这个对应数组位置上的值并不会修改,因此就成了一个谁也不知道是不是有效的threadid(99.9%是一个无效id, 但不排除你人品爆发它可能是一个有效的系统id),然后你pthread_join它...... 生活是万花筒.

另外据我的经验,即使已经成功create的threads函数中的malloc也不会都成功,那么这就意味着有某些thread是必然会非法内存访问的,这也就是说某些thread中已经成功malloc的内存必然会泄漏,因为非法内存访问必定中断thread运行,所以其后的所有free或者delete代码没有机会执行了。

------解决方案--------------------

引用:but I just want to know why the VIRT and DATA keep on growing after those relevant threads quit, I think those exception definition is not related to my purpose,I know my code is not that  well, but this code is just for testing,

first of all pthread_xxxx functions will never throw any C++ exceptions. You shouldn't expect to determine the exit status of your program by exceptions.

second, all your threads are joinable, which means the system has to keep certain amount of RAM for each of them, so your pthread_join can then pick up the exit status of threads.

http://www.ibm.com/developerworks/library/l-memory-leaks/

If each thread does take 10MB reservation for the worst scenario, then your 2K threads will be 20GB. They will not be released until the pthread_join is called an each of them. In other words, even you think the thread is terminated, the memory will not be released until pthread_join is called.

Solution: detached thread. See the url above for more information

Third, as someone indicated above, it's related to the implementation of malloc in c library. It probably has a memory pool to back it up. If there is no free node available, the pool will expand itself. I am not surprised the expansion will take twice the space it had.

Fourth, the implementation of operating system, I don't think I am qualified to explain anything for this regard. But I am pretty sure it will contributes to what you had.

So, short answer: try detached thread

recommendation: I respect the fact that you tried to dig into bottom of this. But, is it really worth you time? A year after, if you revist this question, you could feel you time was a waste, well, not to mention the time I spend here (more than 20 minutes so far). You know why? you will probably have no chance at all to write this kind of codes in your career. My lesson taught me well: you are luck if you have a job doing whatever you like to do. But, in reality, most of people don't. You see, if I have a dream job, why I wast my time here with you? No offense, this applies to a lot of people who wander around here...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值