linux下多线程编程,每创建一个新的线程后VIRT都会增加,即swap增加,但是销毁线程后如何回收增加的swap?

wkl277154604 2014-07-10 09:35:14
第一张图中的TOP中,是在该进程中开了10个线程的效果,可以看出VIRT 为94m

第二张图中的TOP中,是最先开启的10个线程都撤消后,又开辟的10个线程,可以看出VIRT 为153m,可以看出前面的线程撤消后,并没有回收资源(代码中是使用
//kill the idle thread and free info struct
kill(this->thread_info[this->cur_th_num-1].thread_id, SIGKILL);) 关闭线程,
其中线程函数一开始调用了pthread_detach(pthread_self())改为
pthread_detach(pthread_self());//pthread_detach(pthread_self()),将状态改为unjoinable状态,确保资源的释放


哪个大神告诉我,怎么释放进程为前面已经结束了的10线程产生的swap空间呢?我是初学者
...全文
313 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wkl277154604 2014-07-12
  • 打赏
  • 举报
回复
TO 12 楼 嗯嗯 是用的join的方式,和kill实现的,但是 1楼 说了 kill不是好的管理线程的方式, 我也想 尝试换换的,但是现在基本功能实现了 等后面有时间了在多尝试下,把这个方面内容弄透彻点, 我也没有再去验证 自行剥离的方式,不过你有兴趣也可以 按照6楼的方法试试 pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, stacksize); pthread_attr_setschedpolicy(&attr, SCHED_RR); pthread_attr_getschedparam(&attr, ¶m);[/quote]
whut_lcy 2014-07-12
  • 打赏
  • 举报
回复
不过还是没说清楚 为何线程自行剥离(detach)情况下,资源无法自动回收。 to LZ:你采用join的方式,还是用的kill来终止这个线程吗?
  • 打赏
  • 举报
回复
引用 7 楼 wkl277154604 的回复:
嗯嗯 ,谢谢,是的 资源是够的,但是我做的是一个信息通信服务器的形式,它可能某个时段需要开启大量的通信通道,即大量的线程来处理通信,但是有些时候信息量小的时候,只需要一两个线程,所以想到用线程池的概念,所以就去网上下了个模版,但是发现有许多bug,改的过程中就出现了上面的问题,就是当空闲的时候需要将动态增加的线程资源回收。 1楼已经解决了我的问题,我现在也是用pthread_join的方式实现的回收。不过还是谢谢你了哈
O'Reilly <Pthreads Programming> Chapter 3, simple implementation of a threading pool
  • 打赏
  • 举报
回复
kill is not the correct way to manage thread in pthread context. pthread actually offers more elegant approach, such as conditional variables, my favorite https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal
xujianxiang 2014-07-11
  • 打赏
  • 举报
回复
你能开10个线程说明你有资源,你有10线程的资源,你干嘛要回收,下次再建10的时候再分配???这不多此一举,我知道linux默认的线程分配是8M,这样的分配256个线程就2个G了,你考虑的是减少线程分配的资源,而不是傻不垃圾的回收内存。 pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, stacksize); pthread_attr_setschedpolicy(&attr, SCHED_RR); pthread_attr_getschedparam(&attr, ¶m);
wkl277154604 2014-07-11
  • 打赏
  • 举报
回复
引用 6 楼 xujianxiang 的回复:
你能开10个线程说明你有资源,你有10线程的资源,你干嘛要回收,下次再建10的时候再分配???这不多此一举,我知道linux默认的线程分配是8M,这样的分配256个线程就2个G了,你考虑的是减少线程分配的资源,而不是傻不垃圾的回收内存。 pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, stacksize); pthread_attr_setschedpolicy(&attr, SCHED_RR); pthread_attr_getschedparam(&attr, ¶m);
怎么给分
wkl277154604 2014-07-11
  • 打赏
  • 举报
回复
引用 5 楼 micropentium6 的回复:
pthread_detach doesn't necessarily guarantee there is any cleanup handler will be invoked if the thread is terminated in an abnormal way. BTW, I won't use kill in threading environment. use pthread_kill seems better, but I won't do that either. Note that pthread_kill() only causes the signal to be handled in the context of the given thread; the signal action (termination or stopping) affects the process as a whole.
Because the threads I created are processing each one using a while() loop. I need create a dedicated thread to manage them. It is the dedicated thread that kill other threads. This is why I use kill(); And as you have mentioned that the signal action (termination or stopping) affects the process as a whole, but I just want to close the thread, so I don't pthread_kill(). Thank for your reply. I have already solve the problem by using your advice.
wkl277154604 2014-07-11
  • 打赏
  • 举报
回复
嗯嗯 ,谢谢,是的 资源是够的,但是我做的是一个信息通信服务器的形式,它可能某个时段需要开启大量的通信通道,即大量的线程来处理通信,但是有些时候信息量小的时候,只需要一两个线程,所以想到用线程池的概念,所以就去网上下了个模版,但是发现有许多bug,改的过程中就出现了上面的问题,就是当空闲的时候需要将动态增加的线程资源回收。 1楼已经解决了我的问题,我现在也是用pthread_join的方式实现的回收。不过还是谢谢你了哈
  • 打赏
  • 举报
回复
first of all, make sure your program doesn't have any memory leak, second, by default, all threads created by pthread_create are joinable, If you create a joinable thread but forget to join it, its resources or private memory are always kept in the process space and never reclaimed. Always join the joinable threads; by not joining them, you risk serious memory leaks. For joinable threads, the system allocates private storage to store thread termination status. The status is updated after the thread terminates. To retrieve the thread termination status, call pthread_join(pthread_t thread, void** value_ptr). The system allocates underlying storage for each thread, including stack, thread ID, thread termination status, and so on. This underlying storage will remain in the process space (and not be recycled) until the thread has terminated and has been joined by other threads.
  • 打赏
  • 举报
回复
pthread_detach doesn't necessarily guarantee there is any cleanup handler will be invoked if the thread is terminated in an abnormal way. BTW, I won't use kill in threading environment. use pthread_kill seems better, but I won't do that either. Note that pthread_kill() only causes the signal to be handled in the context of the given thread; the signal action (termination or stopping) affects the process as a whole.
xujianxiang 2014-07-10
  • 打赏
  • 举报
回复
pthread_detach(pthread_self());快点给分,分配了不能收回,关掉再分配不会增加
阿呆_ 2014-07-10
  • 打赏
  • 举报
回复
引用
Thank you for your reply. The problem has been solved, as your advice. But I also want to know why I couldn't solve it by using following way. But I already call pthread_detach(pthread_self());//pthread_detach(pthread_self()) in the first line of my thread function. So I think the thread attribute is unjoinable, and I call kill(this->thread_info[this->cur_th_num-1].thread_id, SIGKILL) to kill the thread. The thread will quit and free the memory by itself.
你确信当你kill的时候线程已经执行完了并且已经释放了它malloc的内存了吗?
wkl277154604 2014-07-10
  • 打赏
  • 举报
回复
Thank you for your reply. The problem has been solved, as your advice. But I also want to know why I couldn't solve it by using following way. But I already call pthread_detach(pthread_self());//pthread_detach(pthread_self()) in the first line of my thread function. So I think the thread attribute is unjoinable, and I call kill(this->thread_info[this->cur_th_num-1].thread_id, SIGKILL) to kill the thread. The thread will quit and free the memory by itself.

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧