How To Make ‘memory-eater’ program in C++

If you don’t program for fun, why the hell do you program anyways? : Some American Guy

Did you know your computer has limited memory? Of course you do. I mean most of us has experienced  ‘frozen’ machine at least once in our lives.

Any program you run on your machine enjoys a chunk of available memory which is allotted to it by the Operating System and this chain of co-existence of processes in the memory pool keeps going on until there is enough memory for each one of them. But what happens if this pool dries up? When almost all the memory is used? Yes, you have it right, the machine freezes and the Operating System tries to free up memory by killing not-so-important processes.

A frozen machine is not so fun when you have important things to do with it. But what if you froze the machine on purpose? That sounds fun huh?

So I wrote this Extremely simple program in C++ which eats up gigantic chunks of memory and freeze your system.(One of my friends have an old Pentium PC which froze for almost 20 minutes when we tested this code on it 😛 )

 

#include <iostream>                                //Not necessary , Just By convention
int main()
{
while(true)
int*x=new int;
}

Explanation: What this does is that it starts an infinite loop and keeps allocating memory to an integer, which is 2 or 4 bytes. The program starts eating memory until there is none left and your system starts behaving wierd unless your operating system is forced to kill it.

PS : Why isn’t there a ‘return 0’ statement? The chain of command never reaches past the fifth line!

2 Comments

  1. Petros June 22, 2016
    • Abhishek June 22, 2016