我为内存泄漏压力开发了一个小应用程序:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int period = 0;
int size = 40000001;
char *buf = NULL;
if (argc > 1) period = atoi(argv[1]);
if (period == 0) period = 21;
for (;;) {
buf = malloc(size);
if (buf == NULL) printf ("malloc return NULL\n");
sleep(period);
}
return 0;
}
应用程序每20秒进行一次~40M的内存分配(无空闲)。我的系统内存是~256M。
我使用top命令检查了压力应用程序消耗的内存:
$top | grep "stress"
873 5689 root S 39892 16% 0% ./stress_test
873 5689 root S 39892 16% 0% ./stress_test
873 5689 root S 39892 16% 0% ./stress_test
873 5689 root S 78956 31% 0% ./stress_test
873 5689 root S 78956 31% 0% ./stress_test
873 5689 root S 78956 31% 0% ./stress_test
873 5689 root S 78956 31% 0% ./stress_test
873 5689 root S 78956 31% 0% ./stress_test
873 5689 root S 115m 47% 0% ./stress_test
873 5689 root S 115m 47% 0% ./stress_test
873 5689 root S 115m 47% 0% ./stress_test
873 5689 root S 115m 47% 0% ./stress_test
873 5689 root S 153m 62% 0% ./stress_test
873 5689 root S 153m 62% 0% ./stress_test
873 5689 root S 153m 62% 0% ./stress_test
1061 5689 root S 1564 1% 0% grep stress
873 5689 root S 153m 62% 0% ./stress_test
873 5689 root S 191m 77% 0% ./stress_test
873 5689 root S 191m 77% 0% ./stress_test
873 5689 root S 191m 77% 0% ./stress_test
873 5689 root S 191m 77% 0% ./stress_test
873 5689 root S 229m 93% 0% ./stress_test
873 5689 root S 229m 93% 0% ./stress_test
873 5689 root S 229m 93% 0% ./stress_test
873 5689 root S 229m 93% 0% ./stress_test
873 5689 root S 267m 108% 0% ./stress_test
873 5689 root S 267m 108% 0% ./stress_test
873 5689 root S 267m 108% 0% ./stress_test
873 5689 root S 267m 108% 0% ./stress_test
873 5689 root S 305m 124% 0% ./stress_test
873 5689 root S 305m 124% 0% ./stress_test
873 5689 root S 305m 124% 0% ./stress_test
873 5689 root S 305m 124% 0% ./stress_test
873 5689 root S 305m 124% 0% ./stress_test
873 5689 root S 344m 139% 0% ./stress_test
873 5689 root S 344m 139% 0% ./stress_test
873 5689 root S 344m 139% 0% ./stress_test
873 5689 root S 344m 139% 0% ./stress_test
873 5689 root S 382m 155% 0% ./stress_test
873 5689 root S 382m 155% 0% ./stress_test
1061 5689 root S 1564 1% 0% grep stress
873 5689 root S 382m 155% 0% ./stress_test
873 5689 root S 382m 155% 0% ./stress_test
873 5689 root S 420m 170% 0% ./stress_test
873 5689 root S 420m 170% 0% ./stress_test
873 5689 root S 420m 170% 0% ./stress_test
873 5689 root S 458m 185% 0% ./stress_test
873 5689 root S 458m 185% 0% ./stress_test
873 5689 root S 458m 185% 0% ./stress_test
873 5689 root S 458m 185% 0% ./stress_test
873 5689 root S 496m 201% 0% ./stress_test
873 5689 root S 496m 201% 0% ./stress_test
873 5689 root S 496m 201% 0% ./stress_test
873 5689 root S 496m 201% 0% ./stress_test
873 5689 root S 534m 216% 0% ./stress_test
873 5689 root S 534m 216% 0% ./stress_test
873 5689 root S 534m 216% 0% ./stress_test
873 5689 root S 534m 216% 0% ./stress_test
873 5689 root S 534m 216% 0% ./stress_test
873 5689 root S 573m 232% 0% ./stress_test
873 5689 root S 573m 232% 0% ./stress_test
873 5689 root S 573m 232% 0% ./stress_test
1061 5689 root S 1564 1% 0% grep stress
873 5689 root S 573m 232% 0% ./stress_test
873 5689 root S 611m 247% 0% ./stress_test
873 5689 root S 611m 247% 0% ./stress_test
873 5689 root S 611m 247% 0% ./stress_test
873 5689 root S 611m 247% 0% ./stress_test
873 5689 root S 649m 263% 0% ./stress_test
873 5689 root S 649m 263% 0% ./stress_test
873 5689 root S 649m 263% 0% ./stress_test
873 5689 root S 649m 263% 0% ./stress_test
873 5689 root S 687m 278% 0% ./stress_test
873 5689 root S 687m 278% 0% ./stress_test
873 5689 root S 687m 278% 0% ./stress_test
873 5689 root S 687m 278% 0% ./stress_test
873 5689 root S 725m 293% 0% ./stress_test
873 5689 root S 725m 293% 0% ./stress_test
873 5689 root S 725m 293% 0% ./stress_test
873 5689 root S 725m 293% 0% ./stress_test
873 5689 root S 763m 309% 0% ./stress_test
873 5689 root S 763m 309% 0% ./stress_test
873 5689 root S 763m 309% 0% ./stress_test
873 5689 root S 763m 309% 0% ./stress_test
873 5689 root S 763m 309% 0% ./stress_test
873 5689 root S 801m 324% 0% ./stress_test
873 5689 root S 801m 324% 0% ./stress_test
873 5689 root S 801m 324% 0% ./stress_test
873 5689 root S 801m 324% 0% ./stress_test
873 5689 root S 840m 340% 0% ./stress_test
873 5689 root S 840m 340% 0% ./stress_test
1061 5689 root S 1564 1% 0% grep stress
873 5689 root S 840m 340% 0% ./stress_test
1061 5689 root S 1564 1% 0% grep stress
873 5689 root S 840m 340% 0% ./stress_test
Mem: 120004K used, 132592K free, 0K shrd, 0K buff, 30076K cached
873 5689 root S 878m 355% 0% ./stress_test
873 5689 root S 878m 355% 0% ./stress_test
873 5689 root S 878m 355% 0% ./stress_test
873 5689 root S 878m 355% 0% ./stress_test
873 5689 root S 916m 370% 0% ./stress_test
873 5689 root S 916m 370% 0% ./stress_test
873 5689 root S 916m 370% 0% ./stress_test
873 5689 root S 916m 370% 0% ./stress_test
873 5689 root S 954m 386% 0% ./stress_test
873 5689 root S 954m 386% 0% ./stress_test
873 5689 root S 954m 386% 0% ./stress_test
873 5689 root S 954m 386% 0% ./stress_test
1061 5689 root S 1564 1% 0% grep stress
873 5689 root S 992m 401% 0% ./stress_test
873 5689 root S 992m 401% 0% ./stress_test
873 5689 root S 992m 401% 0% ./stress_test
873 5689 root S 992m 401% 0% ./stress_test
873 5689 root S 992m 401% 0% ./stress_test
873 5689 root S 1030m 417% 0% ./stress_test
873 5689 root S 1030m 417% 0% ./stress_test
873 5689 root S 1030m 417% 0% ./stress_test
873 5689 root S 1030m 417% 0% ./stress_test
873 5689 root S 1068m 432% 0% ./stress_test
873 5689 root S 1068m 432% 0% ./stress_test
873 5689 root S 1068m 432% 0% ./stress_test
873 5689 root S 1068m 432% 0% ./stress_test
873 5689 root S 1107m 448% 0% ./stress_test
873 5689 root S 1107m 448% 0% ./stress_test
873 5689 root S 1107m 448% 0% ./stress_test
873 5689 root S 1107m 448% 0% ./stress_test
873 5689 root S 1145m 463% 0% ./stress_test
873 5689 root S 1145m 463% 0% ./stress_test
873 5689 root S 1145m 463% 0% ./stress_test
873 5689 root S 1145m 463% 0% ./stress_test
873 5689 root S 1183m 478% 0% ./stress_test
873 5689 root S 1183m 478% 0% ./stress_test
873 5689 root S 1183m 478% 0% ./stress_test
873 5689 root S 1183m 478% 0% ./stress_test
873 5689 root S 1221m 494% 0% ./stress_test
873 5689 root S 1221m 494% 0% ./stress_test
873 5689 root S 1221m 494% 0% ./stress_test
873 5689 root S 1221m 494% 0% ./stress_test
1061 5689 root S 1564 1% 0% grep stress
873 5689 root S 1221m 494% 0% ./stress_test
873 5689 root S 1259m 509% 0% ./stress_test
873 5689 root S 1259m 509% 0% ./stress_test
873 5689 root S 1259m 509% 0% ./stress_test
1061 5689 root S 1564 1% 0% grep stress
873 5689 root S 1259m 509% 0% ./stress_test
873 5689 root S 1297m 525% 0% ./stress_test
873 5689 root S 1297m 525% 0% ./stress_test
873 5689 root S 1297m 525% 0% ./stress_test
873 5689 root S 1297m 525% 0% ./stress_test
873 5689 root S 1336m 540% 0% ./stress_test
873 5689 root S 1336m 540% 0% ./stress_test
873 5689 root S 1336m 540% 0% ./stress_test
873 5689 root S 1336m 540% 0% ./stress_test
873 5689 root S 1374m 556% 0% ./stress_test
873 5689 root S 1374m 556% 0% ./stress_test
873 5689 root S 1374m 556% 0% ./stress_test
873 5689 root S 1374m 556% 0% ./stress_test
873 5689 root S 1412m 571% 0% ./stress_test
873 5689 root S 1412m 571% 0% ./stress_test
873 5689 root S 1412m 571% 0% ./stress_test
873 5689 root S 1412m 571% 0% ./stress_test
873 5689 root S 1450m 586% 0% ./stress_test
873 5689 root S 1450m 586% 0% ./stress_test
873 5689 root S 1450m 586% 0% ./stress_test
873 5689 root S 1450m 586% 0% ./stress_test
873 5689 root S 1450m 586% 0% ./stress_test
873 5689 root S 1488m 602% 0% ./stress_test
1061 5689 root S 1564 1% 0% grep stress
873 5689 root S 1488m 602% 0% ./stress_test
873 5689 root S 1488m 602% 0% ./stress_test
873 5689 root S 1488m 602% 0% ./stress_test
873 5689 root S 1526m 617% 0% ./stress_test
873 5689 root S 1526m 617% 0% ./stress_test
873 5689 root S 1526m 617% 0% ./stress_test
873 5689 root S 1526m 617% 0% ./stress_test
873 5689 root S 1564m 633% 0% ./stress_test
873 5689 root S 1564m 633% 0% ./stress_test
873 5689 root S 1564m 633% 0% ./stress_test
873 5689 root S 1564m 633% 0% ./stress_test
873 5689 root S 1603m 648% 0% ./stress_test
873 5689 root S 1603m 648% 0% ./stress_test
873 5689 root S 1603m 648% 0% ./stress_test
873 5689 root S 1603m 648% 0% ./stress_test
873 5689 root S 1641m 664% 0% ./stress_test
873 5689 root S 1641m 664% 0% ./stress_test
1061 5689 root S 1564 1% 0% grep stress
873 5689 root S 1641m 664% 0% ./stress_test
873 5689 root S 1641m 664% 0% ./stress_test
873 5689 root S 1679m 679% 0% ./stress_test
873 5689 root S 1679m 679% 0% ./stress_test
873 5689 root S 1679m 679% 0% ./stress_test
873 5689 root S 1679m 679% 0% ./stress_test
873 5689 root S 1679m 679% 0% ./stress_test
873 5689 root S 1717m 694% 0% ./stress_test
873 5689 root S 1717m 694% 0% ./stress_test
873 5689 root S 1717m 694% 0% ./stress_test
873 5689 root S 1717m 694% 0% ./stress_test
873 5689 root S 1755m 710% 0% ./stress_test
873 5689 root S 1755m 710% 0% ./stress_test
873 5689 root S 1755m 710% 0% ./stress_test
873 5689 root S 1755m 710% 0% ./stress_test
873 5689 root S 1793m 725% 0% ./stress_test
873 5689 root S 1793m 725% 0% ./stress_test
873 5689 root S 1793m 725% 0% ./stress_test
873 5689 root S 1793m 725% 0% ./stress_test
873 5689 root S 1831m 741% 0% ./stress_test
873 5689 root S 1831m 741% 0% ./stress_test
873 5689 root S 1831m 741% 0% ./stress_test
873 5689 root S 1831m 741% 0% ./stress_test
1061 5689 root S 1564 1% 0% grep stress
873 5689 root S 1870m 756% 0% ./stress_test
873 5689 root S 1870m 756% 0% ./stress_test
1061 5689 root S 1564 1% 0% grep stress
873 5689 root S 1870m 756% 0% ./stress_test
873 5689 root S 1870m 756% 0% ./stress_test
873 5689 root S 1870m 756% 0% ./stress_test
873 5689 root S 1908m 771% 0% ./stress_test
873 5689 root S 1908m 771% 0% ./stress_test
873 5689 root S 1908m 771% 0% ./stress_test
873 5689 root S 1908m 771% 0% ./stress_test
873 5689 root S 1946m 787% 0% ./stress_test
873 5689 root S 1946m 787% 0% ./stress_test
873 5689 root S 1946m 787% 0% ./stress_test
873 5689 root S 1946m 787% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
1061 5689 root S 1564 1% 0% grep stress
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
873 5689 root S 1984m 802% 0% ./stress_test
第四列是应用程序消耗的内存,第五列是应用程序消耗的内存百分比
因此压力应用程序在内存使用量和内存大小上的增长大于内存大小:1984M(内存的802%)和应用程序继续运行并分配消耗的内存> 100%。但它到达时会停止分配并继续运行
28308 5682 root S 2022m 818% 0% ./stress_test
我预计我的应用程序会在达到100%时停止分配。
你能解释一下这种行为吗?
答案 0 :(得分:5)
为什么你会期望它&#34;崩溃&#34;?如果分配失败,malloc()
只会返回NULL
。
另外,请注意许多现代操作系统(如Linux)通常overcommit内存,并且由于您的代码实际上从未使用分配的内存,因此它可能会很好地实现在用完虚拟空间并强制malloc()
失败之前进行处理。这可能是您看到程序使用的RAM超出可用范围的原因。
当然,您没有说出您有多少交换,这也会影响发生的事情,因为即使您的特定操作系统没有过度使用,它也很可能使用交换。
答案 1 :(得分:0)
当分配实际失败后尝试使用内存时会发生崩溃。 (其他原因包括解除引用狂野指针,尝试访问属于其他进程的内存等)但如果由于内存不可用而导致分配失败,那么单独就不会导致崩溃。