C 内存测试
#include#include #include #include main( int argc, char ** argv ){ char * pTest=0; long iSize; if( 2 == argc ){ iSize = atoi( argv[1] ); } else{ printf("Usage: eatmem \n"; return 1; } // if( NULL != ( pTest = (char*) malloc( iSize*sizeof(char) ))) if( NULL != ( pTest = new char[ iSize ] )) printf( "Eat memory %ld\n", iSize ); else printf( "No such more memory!\n" ); while(1){ memset(pTest,0,iSize); sleep(10); };}
先查看系统的空闲内存 , 约 1.8G
# free total used free shared buffers cached Mem: 2061652 194896 1866756 0 13872 23648 -/+ buffers/cache: 157376 1904276 Swap: 4096312 141040 3955272 运行 eatmem 这个程序, 吃掉500M ./eatmem 500000000 & 再看系统的空闲内存, 少了500M 左右. # free total used free shared buffers cached Mem: 2061652 684476 1377176 0 14024 23648 -/+ buffers/cache: 646804 1414848 Swap: 4096312 141036 3955276 退出eatmem 再看, 内存几乎都回来了. [root@l149 root]# free total used free shared buffers cached Mem: 2061652 195600 1866052 0 14064 23648 -/+ buffers/cache: 157888 1903764 Swap: 4096312 141036 3955276又做了一下试验....写个2G 的文件试试...
[root@l149 root]# free total used free shared buffers cached Mem: 2061652 197080 1864572 0 14276 23648 -/+ buffers/cache: 159156 1902496 Swap: 4096312 141036 3955276 [root@l149 root]# dd if=/dev/zero of=2000M.file bs=10485760 count=200 200+0 records in 200+0 records out [root@l149 root]# free total used free shared buffers cached Mem: 2061652 2043584 18068 0 14332 1874308 -/+ buffers/cache: 154944 1906708 Swap: 4096312 141036 3955276 嘿嘿, 一写完文件, Free memory 就没有了. 有趣的是, 把这个文件一删除, Free memory 就又回来了.