Mac cannot get to sleep properly My Macbook Pro 15" Yosemite (model 3 years ago) has a problem with sleep mode.
I even formatted the disk and reinstalled OS X. 

When I click the Apple icon and choose sleep from the menu, the mouse can move around (within 3-5 pixels) but the display is not turning off.

I wait for perhaps 30 seconds, the display then goes off but the Macbook does not sleep properly. When hitting the power button, the screen shows the Apple icon with the loading bar at 80%.

The computer then freezes, I cannot do anything except force shutdown.

This occurs even when no applications are opened in that session. I have no idea how to debug or troubleshoot this problem.

Please advise.
Found out that the MEMORY is broken.

To troubleshoot this kind of problem, running the Apple Hardware Test
by pressing D or Command-D after restart the Mac OSX.