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

前端 未结 2 800
我寻月下人不归
我寻月下人不归 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:00

    Crash will happen when you try to use memory after when allocation actually fails. (other reasons include dereferencing wild pointer, trying to access memory that belongs to other process etc.) but if allocation fails due to unavailability of memory, that alone will not result in crash.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题