What Causes Cramping in Legs at Night or During Exercise

Leg cramps are caused by sudden, involuntary muscle contractions, and the triggers range from something as simple as dehydration to underlying conditions like nerve damage or poor circulation. Most leg cramps hit the calf muscles, though the thighs and feet are common targets too. In many cases, no single cause is identified, but understanding the most likely culprits can help you figure out what’s behind yours.

Dehydration and Electrolyte Imbalance

Low levels of key minerals, particularly magnesium, potassium, calcium, and sodium, are one of the most widely recognized triggers for leg cramps. These minerals help your muscles contract and relax normally. When they drop too low, muscle fibers can become hyperexcitable and fire on their own, producing that painful, locked-up sensation.

You lose electrolytes through sweat, so cramps are especially common during hot weather or intense physical activity. But you don’t have to be an athlete to run low. Poor diet, vomiting, diarrhea, and certain medications (particularly diuretics, or “water pills”) can all deplete your electrolyte stores. If your cramps come with other signs like general weakness, numbness, tingling, or confusion, an electrolyte imbalance is worth investigating with a blood test.

Exercise and Muscle Fatigue

Cramps during or shortly after physical activity are extremely common, and two main mechanisms explain why. The first is the electrolyte and dehydration pathway described above: heavy sweating pulls sodium and chloride out of your body, which causes fluid to shift away from the spaces surrounding your muscle cells. That shift can irritate nearby nerve endings and trigger spontaneous contractions.

The second explanation focuses on muscle fatigue itself. When a muscle is overworked or held in a shortened position for too long, the normal balance between signals that tell the muscle to contract and signals that tell it to relax gets disrupted. The “contract” signals from sensors inside the muscle start overpowering the “relax” signals, and the muscle locks up. This is why cramps tend to strike toward the end of a long run or game rather than at the beginning, and why muscles that are already tired from an unusual amount of activity are the ones most likely to cramp.

Nighttime Leg Cramps

Cramps that wake you from sleep are called nocturnal leg cramps, and they’re especially common in older adults. These cramps typically last from a few seconds to several minutes and leave a sore, tender muscle behind even after the contraction stops.

The exact cause often goes unidentified, but several factors increase the risk. Prolonged sitting or standing during the day, sleeping with your feet pointed downward (which shortens the calf muscles), and general physical inactivity all make nighttime cramps more likely. Underlying health conditions play a role too: kidney disease, diabetic nerve damage, poor blood flow from peripheral artery disease, thyroid disorders, and spinal stenosis (narrowing of the spinal canal that compresses nerves) are all linked to nighttime leg cramps.

Poor Circulation

When your leg muscles don’t get enough blood, they don’t get enough oxygen, and that can trigger cramping. Peripheral artery disease, a condition where plaque narrows the arteries supplying your legs, is a classic example. People with this condition often notice cramp-like pain in their calves during walking that eases with rest. High blood pressure and other vascular conditions can contribute to reduced blood flow as well.

Sitting or lying in one position for a long time can also temporarily restrict circulation, which is one reason cramps tend to show up during sleep or after long periods at a desk.

Nerve Compression and Damage

Problems with the nerves supplying your leg muscles are another significant cause. Spinal stenosis can compress the nerves as they exit your lower spine, producing cramping that often worsens with walking. Peripheral neuropathy, which is nerve damage in the legs and feet often caused by diabetes, can also trigger cramps. Pinched nerves in the back or neck and spinal cord injuries are additional sources of nerve-related cramping.

Neurological conditions like Parkinson’s disease can affect muscle control in ways that promote cramping as well.

Medications That Trigger Cramps

A surprisingly long list of common medications can cause leg cramps as a side effect. Statins (cholesterol-lowering drugs) and diuretics are among the most frequently suspected, though the evidence for diuretics is weaker than many people assume. Certain antidepressants, anti-seizure medications, sleep aids, anti-inflammatory drugs like naproxen, estrogen-based hormone therapies, and osteoporosis treatments have all been associated with leg cramps in clinical studies.

If your cramps started or worsened after beginning a new medication, that timing is worth noting. Among the drugs with the highest reported rates, intravenous iron treatments stand out at around 23% of patients, while estrogen-based therapies range from about 4% to 14%. Most other medications cause cramps in fewer than 3% of users.

Pregnancy

Leg cramps are a hallmark complaint during pregnancy, particularly in the second and third trimesters. The exact mechanism isn’t fully understood, but lower calcium levels in the blood during pregnancy may contribute. The added weight, shifting posture, increased blood volume, and pressure on leg nerves from the growing uterus likely all play a role. These cramps are most common at night and usually resolve after delivery.

Does Magnesium Actually Help?

Magnesium supplements are widely marketed as a cramp remedy, but the evidence is underwhelming for most people. A Cochrane review, the gold standard for evaluating medical evidence, found that magnesium supplements made no statistically significant difference in cramp frequency, intensity, or duration compared to a placebo in older adults with nocturnal leg cramps. The researchers concluded that magnesium is unlikely to provide meaningful relief for this group. The story may be slightly different during pregnancy, where some evidence supports a possible benefit, but for the general adult population, magnesium supplements are not the reliable fix they’re often made out to be.

What Helps in the Moment

When a cramp strikes, stretching the affected muscle is the fastest way to stop it. For a calf cramp, pull your toes up toward your shin, either by hand or by standing and pressing your heel into the floor. Walking around for a minute can help as well, since activating the opposing muscle group encourages the cramping muscle to release. Gentle massage and applying heat to the tight muscle can ease lingering soreness afterward.

For prevention, staying hydrated throughout the day, stretching your calves and hamstrings before bed, and keeping physically active are the most consistently supported strategies. If you sit or stand for long stretches, changing positions regularly and moving your legs can help maintain circulation. For people whose cramps are tied to an underlying condition like diabetes, thyroid disease, or peripheral artery disease, managing that condition is the most effective long-term approach.