This is why we get cramps at night

Hold the stretch for several seconds.
Massage the muscle gently.
Walk slowly once it begins to relax.
Warmth may also help, such as a warm shower or heating pad.

Can Stretching Before Bed Help?
Yes, for some people.

A simple calf stretch can be done by:

Facing a wall
Putting one foot behind you
Keeping the heel on the floor
Leaning forward slowly
Holding for 20–30 seconds
Repeat on the other side.

Should You Take Magnesium?
Not automatically.

Magnesium deficiency can cause muscle symptoms, but ordinary nighttime cramps do not