我正在尝试研究基于pthread的多线程代码修改对基于64位Centos的Linux系统上的VIRTUAL内存的影响
我附加了一个电子表格,其中观察是在VIRTUAL MEMORY上进行的(使用基于Linux的top命令),增加了线程数,每个线程分配1MB。
我粘贴用于观察的C ++代码(如下)作为参考:
从下表所示的观察结果来看,对于分配1MB数据的单个线程,使用的VIRTUAL内存为89.824MB,对于2个线程,每个VIRTUAL内存分配1MB为165.604MB,对于3个线程,每个VIRTUAL内存分配1MB为235MB然后,每个线程的VIRTUAL内存增加1MB。
Click here to view the table having observations made
总结一下,VIRTUAL内存以89MB开始,加入第二个线程后变为双倍,加入第三个线程后再变为几乎加倍,然后每个线程增加1MB,每个分配1MB的堆。任何人都可以解释为什么系统会出现这种行为?
如果您需要任何其他详细信息,请发布。期待着帮助。
注意:
代码是用g ++编译的(g ++ -o vma CODE_FILE.cpp -lpthread)
每个线程只分配了1MB的堆,但没有填充
生成的二进制大小只有~9KB(使用ls命令获得)
#include <iostream>
#include <pthread.h>
using namespace std;
/*
NOTE: Make sure you provide NUM_THREADS in the range (1..5)
*/
#define NUM_THREADS 5
struct thread_struct
{
int id;
char *msg;
};
void *thrdFunc(void *i)
{
char *it = (char*)i;
cout << "\nthis is thread named :"<< it ;
char *mallocBlk = new char [1000000];
}
int main()
{
int rc = 0;
void *status;
char threadNames[8][8] = { "Thread1" , "Thread2", "Thread3", "Thread4", "Thread5", "Thread6", "Thread7", "Thread8"};
pthread_t threads[NUM_THREADS];
thread_struct thrStructs[NUM_THREADS];
cout << "\nIn Main, creating threads!" ;
/*
Create threads
*/
for (int i = 0; i < NUM_THREADS; i++)
{
rc = pthread_create(&threads[i], NULL, thrdFunc, &threadNames[i] );
if (rc)
{
cout << "Thrd" << i+1 << " cant be created";
}
}
/*
Wait till all the threads complete
*/
for (int i = 0; i < NUM_THREADS; i++)
{
pthread_join(threads[i], &status);
}
cout << endl;
cout << "Done! Waiting for your input!";
cin.get();
return 0;
}
下面是5个线程的内存映射:
[rebaca @ localhost stackoverflow] $ cat / proc / 31528 / maps 00400000-00401000 r-xp 00000000 fd:02 20451094 / home / rebaca / stackoverflow / vma 00601000-00602000 rw-p 00001000 fd:02 20451094 / home / rebaca / stackoverflow / vma 01784000-017a5000 rw-p 00000000 00:00 0 [堆] 305b400000-305b420000 r-xp 00000000 fd:00 1703944 /lib64/ld-2.12.so 305b61f000-305b620000 r - p 0001f000 fd:00 1703944 /lib64/ld-2.12.so 305b620000-305b621000 rw-p 00020000 fd:00 1703944 /lib64/ld-2.12.so 305b621000-305b622000 rw-p 00000000 00:00 0 305b800000-305b98a000 r-xp 00000000 fd:00 1703966 /lib64/libc-2.12.so 305b98a000-305bb8a000 --- p 0018a000 fd:00 1703966 /lib64/libc-2.12.so 305bb8a000-305bb8e000 r - p 0018a000 fd:00 1703966 /lib64/libc-2.12.so 305bb8e000-305bb90000 rw-p 0018e000 fd:00 1703966 /lib64/libc-2.12.so 305bb90000-305bb94000 rw-p 00000000 00:00 0 305bc00000-305bc17000 r-xp 00000000 fd:00 1703968 /lib64/libpthread-2.12.so 305bc17000-305be17000 --- p 00017000 fd:00 1703968 /lib64/libpthread-2.12.so 305be17000-305be18000 r - p 00017000 fd:00 1703968 /lib64/libpthread-2.12.so 305be18000-305be19000 rw-p 00018000 fd:00 1703968 /lib64/libpthread-2.12.so 305be19000-305be1d000 rw-p 00000000 00:00 0 305c800000-305c883000 r-xp 00000000 fd:00 1704110 /lib64/libm-2.12.so 305c883000-305ca82000 --- p 00083000 fd:00 1704110 /lib64/libm-2.12.so 305ca82000-305ca83000 r - p 00082000 fd:00 1704110 /lib64/libm-2.12.so 305ca83000-305ca84000 rw-p 00083000 fd:00 1704110 /lib64/libm-2.12.so 3066800000-3066816000 r-xp 00000000 fd:00 1704139 /lib64/libgcc_s-4.4.7-20120601.so.1 3066816000-3066a15000 --- p 00016000 fd:00 1704139 /lib64/libgcc_s-4.4.7-20120601.so.1 3066a15000-3066a16000 rw-p 00015000 fd:00 1704139 /lib64/libgcc_s-4.4.7-20120601.so.1 3066c00000-3066ce8000 r-xp 00000000 fd:00 809560 /usr/lib64/libstdc++.so.6.0.13 3066ce8000-3066ee8000 --- p 000e8000 fd:00 809560 /usr/lib64/libstdc++.so.6.0.13 3066ee8000-3066eef000 r - p 000e8000 fd:00 809560 /usr/lib64/libstdc++.so.6.0.13 3066eef000-3066ef1000 rw-p 000ef000 fd:00 809560 /usr/lib64/libstdc++.so.6.0.13 3066ef1000-3066f06000 rw-p 00000000 00:00 0 7fd3ec000000-7fd3ec115000 rw-p 00000000 00:00 0 7fd3ec115000-7fd3f0000000 --- p 00000000 00:00 0 7fd3f0000000-7fd3f0115000 rw-p 00000000 00:00 0 7fd3f0115000-7fd3f4000000 --- p 00000000 00:00 0 7fd3f4000000-7fd3f4115000 rw-p 00000000 00:00 0 7fd3f4115000-7fd3f8000000 --- p 00000000 00:00 0 7fd3f8000000-7fd3f8115000 rw-p 00000000 00:00 0 7fd3f8115000-7fd3fc000000 --- p 00000000 00:00 0 7fd3fc000000-7fd3fc115000 rw-p 00000000 00:00 0 7fd3fc115000-7fd400000000 --- p 00000000 00:00 0 7fd4032b6000-7fd4032b7000 --- p 00000000 00:00 0 7fd4032b7000-7fd403cb7000 rw-p 00000000 00:00 0 7fd403cb7000-7fd403cb8000 --- p 00000000 00:00 0 7fd403cb8000-7fd4046b8000 rw-p 00000000 00:00 0 7fd4046b8000-7fd4046b9000 --- p 00000000 00:00 0 7fd4046b9000-7fd4050b9000 rw-p 00000000 00:00 0 7fd4064bb000-7fd4064c1000 rw-p 00000000 00:00 0 7fd4064db000-7fd4064de000 rw-p 00000000 00:00 0 7ffc72456000-7ffc7246b000 rw-p 00000000 00:00 0 [堆栈] 7ffc72568000-7ffc72569000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]