- 论坛徽章:
- 1
|
看<C专家编程>时,有一练习,程序如下:
- 1 #include <stdio.h>
- 2 #include <stdlib.h>
- 3
- 4 int main(){
- 5 int MB = 0;
- 6 while(malloc(1 << 20)) ++MB;
- 7 printf("Allocated %d MB total.\n", MB);
- 8 }
复制代码
运行该程序,看看进程可以分配多少的内存?
在默认内核下面,结果是:
先看看/usr/src/sys/i386/include/vmparam.h,其中定义的一个进程的最大数据段的大小MAXDSIZ就是512M。
/*
* Virtual memory related constants, all in bytes
*/
#define MAXTSIZ (128UL*1024*1024) /* max text size */
#ifndef DFLDSIZ
#define DFLDSIZ (128UL*1024*1024) /* initial data size limit */
#endif
#ifndef MAXDSIZ
#define MAXDSIZ (512UL*1024*1024) /* max data size */
#endif
#ifndef DFLSSIZ
#define DFLSSIZ (8UL*1024*1024) /* initial stack size limit */
#endif
#ifndef MAXSSIZ
#define MAXSSIZ (64UL*1024*1024) /* max stack size */
#endif
#ifndef SGROWSIZ
#define SGROWSIZ (128UL*1024) /* amount to grow stack */
#endif
继续测试,根据handbook,在内核加入参数:
options MAXDSIZ="(1024*1024*1024)"
options DFLDSIZ="(1024*1024*1024)"
再运行该程序,结果是:
接下来继续,把参数调大到2049看看: (感谢antijp指出先前的错误,现更正)
options MAXDSIZ="(2049ULL*1024*1024)"
结果是:
PS:测试环境:FreeBSD 6.0-RELEASE,内存1GB.
后记:同一程序在Gentoo Linux上运行,结果是:
感谢雨丝风片,antijp,gvim!欢迎大家讨论及指正
[ 本帖最后由 congli 于 2006-3-27 14:14 编辑 ] |
|