You’re midway through a run, workout, or CrossFit session when suddenly it strikes—a debilitating muscle cramp that stops you cold. Nothing kills a workout quite like the pain and tightness a cramp brings on. And whether you’re lifting weights, running sprints, or doing laps in the pool, you can cramp up.

“A cramp is when the muscle unit itself contracts and doesn’t have enough resources to release that contraction,” explains Jennifer J. Beck, M.D., an assistant professor of orthopedic surgery at UCLA’s Orthopedic Institute for Children. Muscle cramps typically strike whatever area you’re working the hardest. “Runners are likely to get lower-extremity cramps, while rowers and swimmers might be more prone to upper-extremity and chest-wall cramps,” Beck says.

Nutrition no-no’s

It’s not just the movement that’s to blame. “The most common cause of muscle cramps tends to be something nutritional,” says Jordan D. Metzl, M.D., a sports medicine physician at the Hospital for Special Surgery in New York. That’s especially true for endurance athletes like distance runners, cyclists, and triathletes, who have lost high amounts of electrolytes through their sweat without replacing them. This can lead to a deficiency in sodium levels, which triggers cramps. “In order to combat these deficiencies you need to make sure you’re being nutritionally smart,” says Metzl. That means getting enough electrolytes in your diet with salty foods like pretzels or taking salt tablets during workouts.

SEE ALSO: The Fit Woman’s Guide to Supplements

In addition to sodium, the minerals potassium, magnesium, and calcium can help prevent your muscles from locking up. Aim to get these nutrients through foods first, advises Beck. Key sources include bananas for potassium, leafy green vegetables for magnesium, and diary products like milk and yogurt for calcium.

Proper hydration is also important, since dehydration causes an alteration in electrolyte concentration as well as a buildup of lactic acid from muscle metabolism, both of which can cause cramps. Individual needs vary—some people do fine with just a little water, others require more fluids or a sports drink with electrolytes. “Experiment to see what works best for you,” says Beck.

Activity overload

Cramping can also occur when your workouts get the best of you. “Functional cramps happen in people who overload their muscular capacity,” says Metzl. That can mean putting too much weight on the bar, maxing out on the number of reps you’re doing, or even putting your body in a position where flexibility is an issue. To help keep muscles mobile, incorporate more flexibility in your training. It can also help to use a foam roller before a workout, adds Metzl.

Sudden and intense movements, such as sprint intervals, may be another cramp trigger. That’s especially true if you haven’t properly warmed up, stretched, or prepared your body for action. “If you’re going to recruit all the energy required of your hamstrings and quad muscles to do something like sprint 400m but your muscles don’t have the energy or building blocks to do so, they will most likely cramp up,” explains Beck.

SEE ALSO: 17 HIIT Workouts to Try When Your Routine Gets Boring

Improving your flexibility can go a long way toward keeping your muscles limber and less likely to cramp. “Tighter muscles have less change in length during normal function, known as excursion,” says Beck. “That means they have to do the same amount of work but with a shorter change in length.” Repeating this movement at a high rate or high volume can cause the muscle to cramp, since it’s not getting properly stretched during exercise, she notes.

Stretching, as well as a proper warmup, is key to avoiding cramps mid-workout. Stretch the muscles you’ll be using the most—for runners, that might mean the hamstrings; for an upper-body strength day, stretch your chest, shoulders, and arms.

If a cramp does strike while you’re exercising, stop and gently stretch the affected muscle. Don’t try to push through the cramp or do a big, dramatic stretch, warns Beck, since either can result in a muscle tear.

Calcium cures

Dairy products like low-fat or fat-free milk supply calcium to help keep muscles from cramping up.

SEE ALSO: The Four Best Supplements for Women