See below. This can be restless leg syndrome, most of the time unknown cause. Happens in 10% of population, more frequent in females. Non-pharmacologic treatments include avoiding caffeine and alcohol, set regular time to go to bed. The are meds also that help this, your dr can help which one will be the best for you.
Leg cramps . Try stretching your legs out before bedtime and stay hydrated. Keep electrolytes/vitmain levels balanced such as with bananas and multivitamins. If they persist see your doctor to make sure it isn't restless legs syndrome or periodic leg movements of sleep. Use of muscle relaxers or dopaminergic agents can help.
Vessels and nerves. If there is intermittent circulation going to a muscle, it is not going to be "fed" properly and the efficiency in the muscle function may be compromised. Twitching is plausible and can result in cramping. Talk to your PCP and a consult with a vascular specialist may be needed, especially if you have other medical history, like diabetes. Read more...