Get memory overflow caused by a memory leak and the application keep running and allocating

前端 未结 2 901
我寻月下人不归
我寻月下人不归 2021-01-27 20:36

I developed a small application for a memory leak stress:

#include 
#include 

int main(int argc, char **argv)
{
    int period =          


        
2条回答
  •  悲哀的现实
    2021-01-27 21:19

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

    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.

提交回复
热议问题