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空间呢?我是初学者
...全文
371 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.
内容概要:本文系统研究了基于CNN-SVM的卷积神经网络与支持向量机融合的数据分类预测方法,聚焦其在工业故障识别中的应用,提供了完整的Matlab代码实现。通过CNN提取输入数据的深层空间特征,再由SVM进行高精度分类,充分发挥两者优势,有效提升了故障识别的准确性、鲁棒性与泛化能力。该方法特别适用于处理电力系统、机械设备等领域的高维、非线性、强噪声监测数据,在变压器故障诊断、轴承缺陷识别等场景中具有重要应用价值。文档还整合了机器学习、深度学习、图像处理、路径规划、电力系统优化等多个前沿科研方向的技术资源,配套大量Matlab/Simulink仿真案例与Python代码,全面支持科研复现与工程实践。; 适合人群:具备一定编程基础,熟练掌握Matlab或Python语言,从事电气工程、自动化、人工智能、机械故障诊断等相关领域研究的研发人员及高校研究生; 使用场景及目标:① 实现工业设备的状态监测与多类别故障分类;② 深入理解CNN与SVM融合模型的设计原理与工程实现细节;③ 借助所提供的丰富算法案例开展科研复现、模型优化与系统仿真验证; 阅读建议:建议按照文档目录结构系统化学习,结合百度网盘提供的完整代码资源进行动手实践,重点关注CNN特征提取层与SVM分类器之间的数据接口设计与参数调优策略,同时可延伸学习文中涉及的其他智能算法及其在电力系统、信号处理等领域的交叉应用,全面提升科研创能力。

23,224

社区成员

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

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