4

I've written the following memory hogging program:

int main() {
    while(1) {
        auto* blah = new char[1024 * 1024 * 1024]{};
    }
    return 0;
}

Which, as far I can tell, reserves a whole GB of memory on every iteration on the loop. I was expecting this to crash almost immediately. But it somehow runs until I stop it. Inspecting my system I discovered that my little program is taking up more memory than available on my machine, how is this possible? what's going on here?

enter image description here

frankelot
  • 13,666
  • 16
  • 54
  • 89
  • Presumably it's immediately getting swapped to [virtual memory](https://en.wikipedia.org/wiki/Virtual_memory) and then, because you never read from any of the giant matrices you create, never swapped back. – Nathan Pierson Mar 07 '21 at 15:58
  • This was my guess, so the OS is supposedly moving memory from RAM to the disk giving the illusion of larger RAM memory, did not know the name for it though! – frankelot Mar 07 '21 at 16:04
  • 2
    (a) virtual memory; (b) virtual address space; (c) pages not being committed until written too – Richard Critten Mar 07 '21 at 16:12

2 Answers2

4

Modern operating systems can be clever about memory allocation and not actually allocate anything until you use the memory you tried to allocate. On such systems malloc and new don't fail when you allocate, but the whole program goes down when you try to use the memory OS told you it allocated even though it physically can't. See this for more on that.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
0

Add blah[1024 * 1024 * 1024 - 1] = 127; after line with new. Probably there is optimisation for unused block of memory. Have you tried to see (printf) the addresses allocated for blah?

i486
  • 6,491
  • 4
  • 24
  • 41
  • 2
    While true, this feels more like a comment. It doesn't actually answer the question. – cigien Mar 07 '21 at 16:04
  • Tried adding the assignment, same results. The program runs indefinitely. – frankelot Mar 07 '21 at 16:10
  • 1
    @ FRR Won't necessarily cause a detectable error for a long time. Accessing only one element of a huge array may result in only the [page](https://en.wikipedia.org/wiki/Page_(computer_memory)) containing the one element actually being allocated to the program. And then this page will go unused and be swapped out, freeing up the memory for a future allocation.. – user4581301 Mar 07 '21 at 16:43
  • Then the assignment can be replaced with `memset( blah, 0, 1024 * 1024 * 1024 );` – i486 Mar 07 '21 at 20:22