centos 6.3 下分析程序虚拟内存偏高的问题

lemonlinger 2012-11-12 03:54:35
两段程序,分别如下:
程序thread.c

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

static void *
thread_start(void *arg)
{
int s = 0;
pthread_attr_t gattr;
pthread_detach(pthread_self());
/*
s = pthread_getattr_np(pthread_self(), &gattr);
*/

while(1){
usleep(100);
}
}

int
main(int argc, char *argv[])
{
pthread_t thr;
int s = 0;
s = pthread_create(&thr, NULL, &thread_start, NULL);

pause();
}

程序thread1.c

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

static void *
thread_start(void *arg)
{
int s = 0;
pthread_attr_t gattr;
pthread_detach(pthread_self());
s = pthread_getattr_np(pthread_self(), &gattr);

while(1){
usleep(100);
}
}

int
main(int argc, char *argv[])
{
pthread_t thr;
int s = 0;
s = pthread_create(&thr, NULL, &thread_start, NULL);

pause();
}

在CentOS 6.2上分别编译成thread和thread1,运行,用top查看如下:

对thread,用pmap查看内存分配情况,如下:
这是pmap -x的结果:

这是pmap -d的结果:

对thread1,用pmap查看内存分配情况,如下:
这是pmap -x的结果:

这是pmap -d的结果:


从top命令的结果可以看出两者的虚拟内存相差将近5倍,可是代码仅仅差一行。
从两者的pmap -x的图里面比较,会发现,比较大的差别是thread1的内存中多了这个:


想请教一下两者的虚拟内存占用会差别这么大?

另外,用pmap查看的时候Mapping列的[anon]是代表什么意思?
还有,Mode列的----代表什么意思,为什么什么权限都没有?

谢谢啦~
...全文
349 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lemonlinger 2012-11-20
  • 打赏
  • 举报
回复
引用 4 楼 lemonlinger 的回复:
有没有高手来解决一下,这是我查到的资料,分享一下,看看有没有帮助。 http://www.linuxquestions.org/questions/linux-kernel-70/virt-memory-jumps-by-64m-900398/ http://bk.kolics.net/?p=57 http://www.quora.com/Why-do-some-a……
答案就在这个链接中: http://www.quora.com/Why-do-some-applications-use-significantly-more-virtual-memory-on-RHEL-6-compared-to-RHEL-5 原因如下: The reason for this is that RHEL 6 includes glibc 2.11 which includes a new "per thread arena memory allocator." This allocator will map many anonymous heaps and try to claim them to be thread-specific. On 64-bit systems, it defaults to allowing up to 8*number_of_cores arenas, and each will be 64M big. This will lead to an extra 4G of virtual memory used for an application with a lot of threads (eg Java servers). 解决办法: This behavior can be somewhat constrained by setting MALLOC_ARENA_MAX=n to set a maximum number of malloc arenas to allocate. 值得注意的是: Note that this is all virtual memory, not committed/resident memory. An application should not use appreciably more resident memory than it did under RHEL 5.
lemonlinger 2012-11-14
  • 打赏
  • 举报
回复
有没有高手来解决一下,这是我查到的资料,分享一下,看看有没有帮助。 http://www.linuxquestions.org/questions/linux-kernel-70/virt-memory-jumps-by-64m-900398/ http://bk.kolics.net/?p=57 http://www.quora.com/Why-do-some-applications-use-significantly-more-virtual-memory-on-RHEL-6-compared-to-RHEL-5
lemonlinger 2012-11-13
  • 打赏
  • 举报
回复
引用 2 楼 qq120848369 的回复:
When the thread attributes object returned by pthread_getattr_np() is no longer required, it should be destroyed using pthread_attr_destroy(3)
加上了,但是问题依旧。
qq120848369 2012-11-12
  • 打赏
  • 举报
回复
When the thread attributes object returned by pthread_getattr_np() is no longer required, it should be destroyed using pthread_attr_destroy(3)
signforlin 2012-11-12
  • 打赏
  • 举报
回复
anon是已分配内存 stack是堆栈 mode:r=read, w=write, x=execute, s=shared, p=private。 获取本线程属性这个,还得大牛来看看。

23,120

社区成员

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

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