I developed a small application for a memory leak stress:
#include
#include
int main(int argc, char **argv)
{
int period =
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.
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.