- 论坛徽章:
- 0
|
#include <iostream>;
#include <string>;
#include <cstdio>;
#include <cstdlib>;
#include <unistd.h>;
#include <sys/types.h>;
#include <sys/stat.h>;
#include <fcntl.h>;
#include <sys/mman.h>;
#include <errno.h>;
using namespace std;
#include <cassert>;
const int fileSize = 1024*1024*100;
const int dataSize = 1024;
int fd = 0;
char *pFileStart = NULL;
void mmapFile()
{
//删除原来的映射区
if (NULL != pFileStart)
{
if (-1 == munmap(pFileStart, fileSize))
{
string sErr("munmap文件映射失败:" ;
sErr += strerror(errno);
cout << sErr << endl;
return;
}
pFileStart = NULL;
cout << "munmap file" << endl;
}
//建立文件映射
pFileStart = (char*)mmap(NULL, fileSize, PROT_WRITE, MAP_SHARED, fd, 0);
if (MAP_FAILED == pFileStart)
{
string sErr("建立文件映射失败:" ;
sErr += strerror(errno);
cout << sErr << endl;
return;
}
cout << "mmap success" << endl;
return;
}
int main(void)
{
char szTmp[256] = {0};
char szData[dataSize] = {0};
char szData2[dataSize] = {0};
for (int i=0; i < 10; i ++)
{
if (fd >; 0)
{
close(fd);
fd = 0;
}
memset(szTmp, 0, sizeof(szTmp));
sprintf(szTmp, "f%d", i);
if ( (fd = open(szTmp, O_CREAT|O_RDWR, 0644)) < 0)
{
string sErr("创建新文件失败:" ;
sErr += strerror(errno);
cout << sErr << endl;
return -1;
}
//修改文件大小
if (-1 == ftruncate(fd, fileSize))
{
//cout << "fileSize = " << m_fileSize <<endl;
string sErr("修改文件大小失败:" ;
sErr += strerror(errno);
cout << sErr << endl;
return -1;
}
//映射文件
mmapFile();
memset(szData, 0, sizeof(szData));
memset(szData, 'a', sizeof(szData)-1);
for (int j=0; j<fileSize/dataSize; j ++)
{
if (NULL == pFileStart)
{
cout << "NULL" << endl;
return -1;
}
memcpy(pFileStart+j*dataSize, "asdf", 4);
//看了半天, 不知道为什么会core
//memcpy(pFileStart+j*dataSize, szData, sizeof(szData));
}
sleep(1);
}
cout << "finished" <<endl;
getchar();
return 0;
}
-----------------------------------------
以上为测试程序
奇怪的问题是程序的运行会把所以的内存耗尽, 不知是为什么 ?请教高手问题出在什么地方 ?
程序退出后内存也不会释放,但删除生成的文件后,内存却会释放, 这是什么道理呢?
以下是用vmstat 1看到的结果
procs memory swap io system cpu
r b w swpd free buff cache si so bi bo in cs us sy id
1 0 0 0 95424 46744 42924 0 0 0 0 109 28 0 6 94
0 0 0 0 95424 46744 42924 0 0 0 0 108 30 0 3 97
0 0 0 0 95424 46760 42924 0 0 0 88 111 40 0 2 98
0 0 0 0 95424 46760 42924 0 0 0 0 113 48 0 1 99
0 0 0 0 95424 46760 42924 0 0 0 0 108 33 0 0 100
1 0 0 0 41284 46760 97196 0 0 0 0 108 35 1 86 13
1 0 2 0 3056 46760 134560 0 0 0 256 115 29 0 100 0
0 0 0 0 38648 46776 145304 0 0 0 52 108 37 0 53 47
1 0 0 0 3056 46776 179864 0 0 0 0 107 33 0 53 47
2 0 1 0 3052 41716 184656 0 0 0 40624 4771 475 0 15 85
1 0 1 11552 4472 25232 200000 0 0 408 152 163 150 0 100 0
0 0 0 11552 3056 21144 206060 0 0 0 0 103 31 0 25 75
1 0 1 11552 3048 16192 211500 0 0 4 53900 2410 149 0 17 83
1 0 0 11552 2296 2068 226596 0 0 0 47824 559 118 0 73 27
1 0 0 11552 2840 2068 226296 0 0 0 0 322 33 3 60 37
1 0 0 11552 2756 2116 226984 0 0 0 40216 496 77 1 33 66
1 0 1 11552 2316 2144 226948 0 0 0 19796 2535 383 1 27 73
1 0 0 11552 2516 2172 226808 0 1208 4 28120 533 46 1 49 50
0 0 1 11552 2756 2172 226560 0 0 0 0 196 26 0 11 89
1 0 2 11552 2364 2208 237912 64 10104 64 40936 457 122 2 61 7
1 0 2 11552 2120 2256 237828 0 0 0 54300 1214 243 0 28 72
procs memory swap io system cpu
r b w swpd free buff cache si so bi bo in cs us sy id
1 0 2 11552 2124 2336 237380 0 0 4 77360 2098 250 0 25 75
0 0 3 11552 2124 2348 237112 0 0 0 9584 474 41 0 21 79
1 0 2 11552 2144 2352 236840 0 0 0 9216 429 92 0 54 46
1 0 2 11552 2148 2384 236356 0 0 0 18912 546 53 2 98 0
0 0 1 11552 2148 2392 236096 0 0 0 6144 648 36 0 18 82
3 0 1 11552 2348 2476 235784 0 0 4 77756 4006 350 0 24 76
0 2 1 11552 2504 2520 235000 0 0 440 21300 518 79 4 84 12
1 1 2 11560 2920 2532 234732 0 0 76 15244 594 29 0 38 62
2 0 3 11552 2172 2600 237080 0 0 4 60064 1957 199 0 17 82
1 0 3 11552 2420 2616 236728 0 0 0 5172 929 182 1 39 61
0 0 3 11552 2660 2620 236460 0 0 0 1396 510 74 1 21 78
1 0 2 11552 3016 2624 236196 0 0 0 4 571 39 0 15 85
1 0 1 11552 2296 2652 237316 0 0 0 29320 450 50 1 38 61
3 0 2 11552 3064 2768 234380 0 0 0 91536 2858 493 0 19 81
1 1 2 11552 2472 2800 236648 64 0 76 16420 978 186 0 15 85
0 2 3 11552 2376 2812 236728 0 0 0 5748 602 49 0 14 86
0 2 3 11552 2372 2816 236728 0 0 0 6140 603 31 0 8 92
0 2 3 11552 2372 2816 236728 0 0 0 4096 609 30 0 4 96
0 2 3 11552 2368 2820 236728 0 0 0 6024 610 45 0 9 91
0 3 3 11552 2296 2820 236776 0 0 48 3976 571 30 0 5 95
0 3 3 11552 2296 2820 236776 0 0 0 4096 536 10 0 12 88
procs memory swap io system cpu
r b w swpd free buff cache si so bi bo in cs us sy id
0 3 3 11552 2296 2824 236776 0 0 0 6112 486 20 1 8 91
1 0 3 11556 2484 2852 236520 64 0 64 31448 976 152 0 22 78
0 1 3 11560 2688 2860 236288 0 0 44 104 487 40 2 26 72
1 0 1 11552 2736 2892 235584 0 0 416 19672 514 85 0 86 14
0 0 1 11552 2512 2924 236416 0 0 0 23204 724 58 0 49 51
0 1 2 11552 2756 2952 236116 20 0 24 29156 573 80 0 16 84
0 1 2 11552 2988 2984 235848 0 0 4 27904 646 82 0 23 77
0 1 2 11552 3188 2988 235776 0 0 12 2676 535 33 0 10 90
0 1 3 11552 3208 2992 235776 0 0 0 64 579 30 0 6 94
0 0 0 11552 3208 3004 235776 0 0 0 80 319 42 0 4 96
0 0 0 11552 3208 3004 235776 0 0 0 0 103 26 0 0 100
1 0 0 11552 3208 3004 235776 0 0 0 0 103 28 0 0 100
0 0 0 11552 3208 3004 235776 0 0 0 0 103 27 0 0 100
0 0 0 11552 3208 3004 235776 0 0 0 0 103 28 0 0 100
--------------------------------------------
删除文件后
procs memory swap io system cpu
r b w swpd free buff cache si so bi bo in cs us sy id
1 0 0 11272 5792 3932 228500 0 0 0 0 103 29 0 1 99
0 0 0 11224 5792 3932 228500 192 0 192 0 115 76 0 5 95
0 0 0 11224 5792 3932 228500 0 0 0 0 112 39 0 3 97
1 0 0 11224 5792 3932 228500 0 0 0 0 107 33 0 2 98
0 0 0 11224 5792 3932 228500 0 0 0 0 108 33 0 2 98
0 0 0 11224 5792 3952 228500 0 0 4 88 113 47 0 1 99
0 0 0 11224 5792 3952 228500 0 0 0 0 109 35 0 0 100
0 0 0 11224 5792 3952 228500 0 0 0 0 104 25 0 0 100
0 0 0 11224 5792 3952 228500 0 0 0 0 103 27 0 1 99
1 0 0 11224 225976 3952 7880 0 0 0 0 108 33 0 59 41
0 0 0 11224 225976 3952 7880 0 0 0 124 107 38 0 0 100
0 0 0 11224 225976 3952 7880 0 0 0 0 103 29 0 1 99
0 0 0 11224 225976 3952 7880 0 0 0 0 103 27 0 0 100
0 0 0 11224 225976 3952 7880 0 0 0 0 103 29 0 0 100
0 0 0 11224 225976 3952 7880 0 0 0 0 103 31 0 0 100
0 0 0 11224 225976 3952 7880 0 0 0 0 104 29 0 2 98
0 0 0 11224 225976 3952 7880 0 0 0 0 103 29 0 0 100
0 0 0 11224 225976 3952 7880 0 0 0 0 103 27 0 0 100
1 0 0 11224 225976 3952 7880 0 0 0 0 103 28 0 1 99
0 0 0 11224 225976 3952 7880 0 0 0 0 103 28 0 1 99
0 0 0 11224 225976 3952 7880 0 0 0 0 103 31 0 2 98 |
|