What do I do when OS X freezes? It happened to me twice recently. I was just using my iMac normally, like browsing the web, and it suddenly freezes. I can move the mouse, but I can't click on anything. Keyboard not functioning at all. I couldn't figure out what to do except for pressing the power button and reboot. What else can I do? Is there a way to diagnose and find out what is causing it?
A strategy to try when the freeze is actually happening is holding ⌘ Cmd and ⌥ Opt, then hitting ⎋ Escape.

If your keyboard input is still working, this should bring up the force-quit dialog (which can be navigated using the arrow keys and Enter).  This may allow you to close problematic applications without a hard shutdown using the ⌽ Power button.