Freebsd 6.2 的服务器, 跑Squid服务,经常在启动不久之后,就会出现
xcalloc: Unable to allocate 1 blocks of 4108 bytes!
错误信息,大致是指Squid申请内存错误。

在 Squid FAQ 中找到
Messages like "FATAL: xcalloc: Unable to allocate 4096 blocks of 1 bytes!" appear when Squid can't allocate more memory, and on most operating systems (inclusive BSD) there are only two possible reasons:

* The machine is out of swap
* The process' maximum data segment size has been reached

The first case is detected using the normal swap monitoring tools available on the platform (pstat on SunOS, perhaps pstat is used on BSD as well).

To tell if it is the second case, first rule out the first case and then monitor the size of the Squid process. If it dies at a certain size with plenty of swap left then the max data segment size is reached without no doubts.

The data segment size can be limited by two factors:

* Kernel imposed maximum, which no user can go above
* The size set with ulimit, which the user can control.

When squid starts it sets data and file ulimit's to the hard level. If you manually tune ulimit before starting Squid make sure that you set the hard limit and not only the soft limit (the default operation of ulimit is to only change the soft limit). root is allowed to raise the soft limit above the hard limit.

文中指出,导致 Squid在 Freebsd下跑起来内存溢出有两种情况:

1、系统的交换分区 Swap分区不够用。

2、进程申请的最大内存受到限制。

该服务器有4G内存,开启了高达8G Swap,明显第一个原因是不对。

然后用top 观察Squid运行状态,发现Squid在跑超过512M内存的时候,就自动退出了,而Squid设置的是1280M的内存参数。

用 limits 查看系统参数,发现单进程的内存占用数被限制在了512M

datasize 524288

通过 ulimt -d 2097152 和 ulimit -Hd 进行强制设置并不成功,因为freebsd默认是csh,不支持ulimit。

继续翻阅Google上的资料,在一个台湾网友那里找到了设置的方法。

kern.maxdsiz=”2G”

添加到

/boot/loader.conf

(中间尝试添加到 /etc/sysctl.conf 重启后不成功)