当前位置: 技术问答>linux和unix
linux下多线程编程,每创建一个新的线程后VIRT都会增加,即swap增加,但是销毁线程后如何回收增加的swap?
来源: 互联网 发布时间:2017-05-23
本文导语: 第一张图中的TOP中,是在该进程中开了10个线程的效果,可以看出VIRT 为94m 第二张图中的TOP中,是最先开启的10个线程都撤消后,又开辟的10个线程,可以看出VIRT 为153m,可以看出前面的线程撤消后,并没有回收资源...
第一张图中的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空间呢?我是初学者
第二张图中的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空间呢?我是初学者
|
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.
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.
|
你能开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);
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, stacksize);
pthread_attr_setschedpolicy(&attr, SCHED_RR);
pthread_attr_getschedparam(&attr, ¶m);