This error occurs when the game does not have enough memory, or RAM, to continue.
RAM stands for "Random Access Memory", which is a vital part of your computer. Your computer will not function without it. It is installed on the computer's motherboard, storing data immediately needed for the computer to function. Minecraft requires at least 384MB of RAM to function, however Minecraft will usually take up more than 1GB of memory to save information for blocks, players, mobs and many other things. When Minecraft cannot allocate (get) more RAM from the computer due to not enough RAM installed or too many other processes using up RAM (such as other games, web browsers and applications), it crashes with the Out of Memory error.
Firstly, you should close every other applications to see if the problem goes away (some web browsers such as Chrome can take up lots of memory if you open a lot of tabs). If not, follow the instructions below.