Make sure you find a magnesium supplement that doesn't contain calcium... basically calcium binds with magnesium and also leeches magnesium from your body to be properly assimilated. Taking magnesium separate from calcium (and calcium rich foods) will keep your levels of magnesium up instead of leeched up by your dietary calcium.
Also, if they come back despite the magnesium, the amino acid Taurine worked wonders for me when I used to get cramps really bad. Don't ask where I found out this, I think it was suggested to me when I had some severe cramps during a summer strongman season and had knotted calves every night. Honestly though, eating tons of meat will give most people the amount of taurine needed.