Hello,
Muscle spasm or cramp , which occur at nightime are also known ar charley Horse.
Muscle cramp may occur due to :
  Overexercise, injury to muscle
  Decreased calcium or 
magnesium in blood, specially during the later preganancy
   Exposure to cold temperatures
   Medical conditions such as thyroid or kidney disease, Blood flow problems such as peripheral arterial disease, and multiple 
sclerosis.   
Dehydration due to decreased intake of fluids
   As side effects of certain medications such as antipsychotics, birth control pills, diuretics, statins and 
steroids.
So , check with your physician if you are on any such medication 
and Increase the intake of fluids and fresh fruit juices to check dehydration .
When spasm occurs:
Stretch your legs/calf muscles or walk around
You may also consider taking a bath to relax your muscles.
For pain , you may take  over the counter drugs such as 
acetaminophen or ibuprofen
Hope this helps,
Regards and best wishes,
Dr.Pallavi Jain