Muscle cramps are involuntary contractions of a muscle, and they are especially disturbing when they occur in the middle of the night. They can be caused by straining or overworking a muscle, dehydration, poor blood supply to the muscle or not getting enough dietary minerals such as potassium, magnesium or calcium. To ease the pain, gently stretch the muscle or walk around a bit to relax the cramp. Massaging the muscle or applying heat can also help relax it. To help prevent leg cramps, drink plenty of fluids; get adequate potassium, magnesium and calcium in your diet; gently stretch your muscles before bedtime; and loosen the sheets at the foot of your bed.