最近听说某省的公司采用的国内某主流磁盘阵列厂家的多路径软件,写日志时,把系统内存慢慢耗光了。
磁盘阵列上是数据库服务器,国产linux操作系统。
今天写了一个小的测试程序,
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define fileName "abc.txt"
int main(void)
{
char *logMsg="Test,make a log.";
int i=0;
while(1)
{
<span style="white-space:pre"> </span>Log("Test:",logMsg,i);
<span style="white-space:pre"> </span>sleep(1);
<span style="white-space:pre"> </span>i++;
}
exit(0);
}
int Log(char *logType,char *logText,int i)
{
FILE *fd = fopen(fileName, "a");
if( NULL == fd)
{
return 0;
}
fprintf(fd, "%d\t%s\n",i, logText);
fflush(fd);
fclose(fd);
}
./a.out之后
不断用top、ps -aux|grep a.out 、free -m 、ls -lrth、lsof不断的进行观察。
结果是这样滴:
测试环境,工作站4G物理内存
用free -m可以看到明显的可用内存在不断往下降,运行小几十分钟后,可用内存从2个多G降为100M以下。
那么这个内存是否是a.out进程使用了呢?
ps -aux查看,a.out进程cpu占有率99%,只占了单核的99%,一共有16核。
但是内存使用基本为0,ps -aux显示为0
top中看到的结果也和ps -aux一样。
ls -lrth看到产生的abc.txt这个时候大小大约为2G了。
lsof |wc 和lsof|grep a.out看结果都基本没变,lsof|grep a.out就5、6个。lsof |wc一直是2700,a.out运行前都是这么多。看来test.c中的fclose关闭是有效的。
最奇怪的是将a.out停下来半个小时了,free -m看内存还没涨起来。
内存去哪儿呢? 请高手指教下。