Mega 2560 randomly hanging

retrolefty:
The ! ASCII character, as in 0x21. So three 0x21 bytes sent in a row anywhere in the upload data stream locks up the bootloader waiting for user monitor commands from the PC which will never come.

Just as an update, the latest Mega2560 bootloader is supposed to have fixed this particular problem. But that is no use to you if have a Mega with the old bootloader installed (unless you want to reflash the bootloader with the newer one.)

However, the latest version seems to have some new issues of its own. Refer this thread:

http://arduino.cc/forum/index.php/topic,136233.0.html