The twitching during sleeping is caused by involuntary muscle spasms when the body relaxes just prior to falling asleep. It may also be a vestigial reflex to prevent our ancestors from falling out of trees that they slept in.
Nearly everyone must have felt the sensation of almost falling asleep only to suddenly jerk awake. In fact nearly 70% to 80% of people have experienced these jerks or twitches at least once in their lives. This jerking and twitching is a common phenomenon however the average person doesn’t remember most of the twitches that occur during the night, which is interesting because for some people this is a nightly occurrence.
Officially, this jerking or twitching is called the hypnagogic jerk or hypnic jerk. Unofficially it is commonly called twitch, sleep twitch, sleep start, or night start. It is basically an involuntary twitch that occurs just as a person is beginning to fall asleep. It often causes the person to awaken suddenly for a moment.
The hypnagogic jerk or hypnic jerk is similar to "jump" experienced by a person when they are startled. It can also be commonly accompanied by a falling sensation, as well as a rapid heartbeat, quickened breathing, sweat, and sometimes "a peculiar sensory feeling of 'shock' or 'falling into the void.'
There are many reasons why the hypnic jerk occurs. This could involve anxiety, stress, alcohol, caffeine, sleep deprivation, heavy exercise late in the evening, sleeping in an uncomfortable position, and being very tired or fatigued.
Researchers aren’t exactly sure how or what causes hypnic jerks, however there are certain theories that try to address the issue. One theory states that the hypnic jerks are a natural part of the body's transition from alertness to sleep. Hence they occur when nerves "misfire" during the process.
Another theory states that the hypnic jerks are part of a vestigial reflex which developed when our ancestors use to sleep in trees. Hence, when the body felt like it might fall out of the tree it fired a hypnic jerks to awaken the person so that they may adjust their sleeping position and prevent falling out of the tree.