出现因内存泄漏和应用程序内存溢出保持运行和分配 [英] Get memory overflow caused by a memory leak and the application keep running and allocating

查看:235
本文介绍了出现因内存泄漏和应用程序内存溢出保持运行和分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个内存泄漏压力的小应用程序:

I developed a small application for a memory leak stress:

#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;
}

有关申请作出40M的〜内存分配(不收费),每20秒。我的系统内存为256M〜

the application make a memory allocation (without free) of ~40M every 20 second. my system memory is ~256M.

我检查的应力应用与top命令消耗的内存:

I checked the memory consumed by the stress application with the top command:

$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

第四列是由应用程序和第5列所消耗的存储器被应用在百分之消耗的存储器

the forth column is the memory consumed by the application and the 5th column is the memory consumed by the application in percent

因此​​,在存储器使用的应力应用的增长和到达值比存储器大小大:1984M(存储器802%)和该应用程序保持运行并分配whenthe消耗的内存是> 100%。但它停止分配并继续运行,当它reachs

So the stress application growth in the memory usage and reach values bigger than the memory size: 1984M (802 % of the memory) and the application keep running and allocating whenthe consumed memory is > 100%. But it stops allocating and keep running when it reachs

28308 5682 root S 2022m 818% 0% ./stress_test

我希望我的应用程序停止分配,当它达到100%。

I expected that my application stop allocating when it reached 100%.

你能解释这种现象?

推荐答案

为什么你会希望它撞车?如果无法分配,的malloc()只返回 NULL

Why would you expect it to "crash"? If it fails to allocate, malloc() just returns NULL.

另外请注意,许多现代的操作系统(如Linux)通常过量内存,而且由于你的code从不真正的使用的分配的内存,它可以由一个很大的可能是这样做的运行的虚拟空间,并迫使前的malloc()失败。为什么你看到使用更多的内存比你的程序是用这是可能的。

Also, note that many modern operating systems (like Linux) typically overcommit the memory, and since your code never actually uses the allocated memory, it can probably do so by a great deal before running out of virtual space and forcing malloc() to fail. This is likely why you see your program using more RAM than is available.

当然,你没有说你有多大的交换有,这也将影响会发生什么,因为即使你特定的操作系统并不过量使用,它很可能使用交换。

Of course, you failed to say how much swap you have, which will also affect what happens since even if you particular OS doesn't overcommit, it very likely uses swap.

这篇关于出现因内存泄漏和应用程序内存溢出保持运行和分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆