1

So I recently got into learning C++ and I was really intrigued by memory allocation, garbage collection, heap, stack, etc...

I wrote this program, and I was wondering what it would do to my computer? Would it crash because my RAM would get filled up? Would it run slower? Would it stop before it fills up too much ram?

I am too afraid of running it on my machine.

#include <iostream>
using namespace std;

int main()
{
    while (true) {
        int* x = new int;
    }
}

EDIT:

So I ran the program on my PC and it is really just a ram eating program. I kept it going until windows explorer failed and my desktop went black. Then I rebooted the PC and it was fine.

heartgg
  • 13
  • 3

2 Answers2

2

It actually depends on the host system on which the program is run, and the compiler.

Some compilers (particularly with optimisation settings enabled) are smart enough to recognize that that the memory allocated in the loop is not used in a way that affects program output. The compiler may then omit the memory allocation and may then (since an infinite loop that does nothing actually yields undefined behaviour in C++) do anything. Possible outcomes then include the program looping forever (consuming CPU cycles but not allocating memory), the compiler not generating the loop at all (so the program immediately terminates).

If compiler optimisation is not in play (e.g. a non-optimised build) the program may loop and repeatedly allocate memory. What happens then depends on the operating system.

Some operating systems - such as unix variants - can be configured (or are configured by default) to do lazy allocation or over-committing, in the sense they won't actually allocate virtual or physical memory until the program tries to non-trivially use that memory (e.g. stores data in it). Since your loop never uses the memory it allocates, on a system doing lazy allocation, the loop may run indefinitely - with no observable effect on the host system, other than lost CPU cycles.

If the OS is not configured to do lazy allocation (e.g. actually allocates the memory immediately) the loop will continue until allocation fails and terminate the program by throwing an exception.

What happens during that process depends, again, on the operating system.

If the operating system imposes a limited quota to the process that is significantly less than the total memory (physical or virtual) available to the operating system, then other processes (and the OS itself) may be unaffected by our program.

If the operating system allows the program to allocate all available memory, then the repeated allocation in this program may affect the behaviour of other programs, or the operating system itself. If the operating system is able to detect that, it may forceably terminate the program - and, in that process, it may reclaim the allocated memory. If the operating system does not detect that behaviour and terminate the program, the program may cause slow-down of the operating system.

If your operating system itself is hosted (e.g. in a hypervisor environment) then the operating system itself may be terminated, but other instances of operating systems running on the same physical machine will normally be unaffected.

It has been at least two decades since a novice programmer could unleash such a program on an unsuspecting machine, and be even a little confident that the machine will eventually slow down and possibly crash, or need a hard power cycle. Operating system design, and larger system designs (e.g. routine use of hypervisors) make such pranks less likely to have any enduring effect.

Peter
  • 35,646
  • 4
  • 32
  • 74
  • 1
    The runtime has to remember the allocation (which may need even more space than the actual int when using `new int`), so lazy allocation may not prevent from running out of memory. – Holger May 31 '21 at 08:44
1

first thing you will face with is memory leak.

but your programme will continue to allocate memory from heap and will face with memory leak but OS will stop your programme. there is concept in OS named OOM. in this concept if some programme continue to allocate memory and cause some important part of system goes down OS will immediately send SIGKILL signal. which is cause you programme killed and then free allocated memory.

N0ll_Boy
  • 500
  • 3
  • 6
  • So you said that after the program is killed with SIGKILL, the memory is freed. Does that mean that all of those allocated "new" integers in the heap that can't be accessed anymore are cleared? Or is a computer restart needed to clear that? – heartgg May 29 '21 at 03:43
  • Actually I think I found the answer to the follow up here https://stackoverflow.com/questions/15882531/does-the-heap-get-freed-when-the-program-exits – heartgg May 29 '21 at 03:45
  • @heartgg dont need to restart. OS will take care of those memory. but note that anytime that you run programme you can kill that programme with something like Ctrl^c, Ctrl^z or command like kill (at least in linux). so dont worry OS will take care of all. – N0ll_Boy May 29 '21 at 03:45
  • The program's process is terminated and cleared and allocated heap memory returned to the OS. – doug May 29 '21 at 03:45
  • Not correct. The C++ standard says you will get an exception in this circumstance; the C standard says `malloc()` and friends will return null; and what exactly is `ENOMEM` for? – user207421 May 29 '21 at 03:58
  • @user207421 you can see here https://stackoverflow.com/questions/10966121/when-should-errno-be-assigned-to-enomem actually OOM killer will go to kill process yes you right in some circumstances(for example try to allocate big vector) c++ will throw std::bad_alloc or something like this. i saw many teams face with OOM killer without getting even exception(i said in some circumstances you will get exception). – N0ll_Boy May 29 '21 at 04:03
  • You didn't say any such thing. The first person to mention exceptions in this thread was me. – user207421 May 29 '21 at 04:08
  • On some popular OSs, the OS will over-allocate when the memory is requested, so no exception will be thrown, but when the allocated pages are accessed and unavailable the program will fault and the OS will kill it. – Eljay May 29 '21 at 04:13