The most common cause of muscle cramps in exercisers is lack of salt, according to a report from the University of Oklahoma. The authors cite studies of tennis and football players showing that crampers tend to be salty sweaters, and of triathletes who cramp losing more salt during a race than peers who did not cramp. They found that intravenous saline can reverse cramping, and that more salt in the diet or in sports drinks can help to prevent heat cramping.
Until now, the leading theory was that most cases of muscle cramps in competitive athletes are caused by an exaggerated "stretch reflex". When you stretch a muscle, it pulls on its tendon. Stretch reflex nerves in that tendon send a message back to the spinal cord (not the brain), and then the "stretch reflex" in the spinal cord sends a message along nerves from the spine to cause the muscle to contract. During fatigue, the muscle retains its contraction to form a cramp. A study from South Africa showed that the most likely cause is muscle fatigue or tearing of the muscle itself. If this is true, muscle cramps during endurance events can be prevented by slowing down when you feel excessive soreness in one muscle group or straining in a muscle. Of course, competitive athletes will not do this, and they pay for it with muscle cramps.
For many years I have recommended eating salted peanuts or other salty foods during heavy exercise, but other doctors believe that extra salt will raise blood pressure. If this is a concern for you, get a wrist cuff monitor and check your blood pressure every day. You are likely to find that your blood pressure goes down, not up, with regular exercise even when you add salt. Journal reference; salt and blood pressure