Re: [問題] 如何把linux的Ram佔光

作者: lf5471 (lf)   2015-02-13 21:08:37
※ 引述《LORDJACK (文亞南)》之銘言:
: 最近公司的linux server有個問題
: 以前裝32G的ram工作正常, 記憶體超過了就用page
: 最近把ram加到64G, 發生奇怪的問題
: 記憶體用量到24G時就會整個系統卡住, 連page也不用了
: 因此我想寫一個程式如下
沒有真的跑, 試看看吧.
簡單說就是一直 fork 到 fork 不了為止
#define SIZE_4GB 0x100000000UL
int g_n = 0;
int main(void)
{
pid_t pid;
char *buf;
pid = fork();
if (pid < 0) // fork fail
{
printf("\nFork fail! => total malloc size = %d (GB)\n", 4*g_n);
return 1;
}
// else, fork success
if (pid == 0) // child process
{
if ((buf = (char *)malloc(SIZE_4GB)) != NULL)
{
memset(buf, 0x00, SIZE_4GB);
g_n++;
printf("child (%6d) : %d\n", pid, g_n);
}
else
printf("child (%6d) : malloc fail! g_n = %d\n", pid, g_n);
}
else
{
//Parent process, do nothing.
printf("parent (%6d) : do nothing\n", getpid());
}
return 0;
}
作者: PkmX (阿貓)   2015-02-13 21:17:00
你這樣只會fork一次吧...
作者: OPIV (Monitor)   2015-02-14 11:11:00
沒記錯的話,現在的linux內核會阻止無窮迴圈fork()...那叫做fork bomb...
作者: lf5471 (lf)   2015-02-14 16:11:00
sorry, TIMEOUT 可加大, 例如 60 (一分鐘)
作者: PkmX (阿貓)   2015-02-14 20:15:00
實在太麻煩了XD...為何不寫一個會吃4G記憶體的程式 然後shell下多開幾個就好XD
作者: dirkc (3781615)   2015-02-15 00:32:00
樓上說的沒錯。另外把記憶體都耗掉是否能有效抓出原來的蟲或許更值得深入討論。這樣的debug方式,在server上風險相當高

Links booklink

Contact Us: admin [ a t ] ucptt.com