Depends on the operating system. For example, on Linux, this really works. See "optimistic memory allocation."
In addition to this, there is a difference between virtual memory and physical memory. On most operating systems, you use virtual memory, so even if you allocate a lot of megabytes of memory, the actual physical memory will only be used after you write it (at this point, what was in this place before can be unloaded back to disk).
There is a "virtual address space", which is basically a "logical" memory, which is then mapped to physical RAM (or to your hard disk if the memory is unloaded).
source share