Counting sheep is a mental exercise used in some European, American and Japanese cultures as a means of lulling oneself to sleep.
In most depictions of the activity, the practitioner envisions an endless series of identical white sheep jumping over a fence, while counting the number that do so. The idea, presumably, is to induce boredom while occupying the mind with something simple, repetitive, and rhythmic, all of which are known to help humans sleep. It may also simulate Rapid Eye Movement, tiring people's eyes.
Although the practice is largely a stereotype, and rarely used as a solution for insomnia, it has been so commonly referenced by cartoons, comic strips, and other mass media, that it has become deeply engrained into popular culture's notion of sleep. The term "counting sheep" has entered the English language as an idiomatic term for insomnia. Sheep themselves have become associated with sleep, or lack thereof. Some examples of fictional counting sheep include the characters in the Serta mattress company's advertisements, a large flock of sheep that have all different numbers written on their sides to convince viewers that if they purchase a Serta mattress, they will no longer have insomnia and will no longer feel the need to count sheep, because often in the commercials in which they appeared the sheep would visit people at night and ask to be counted, only to be turned down. Also, a plush toy of a Serta Counting Sheep often comes with the purchase of a Serta mattress.
According to an experiment conducted by researchers at Oxford University, counting sheep is actually an inferior means of inducing sleep. Subjects who instead imagined "a beach or a waterfall" were forced to expend more mental energy, and fell asleep faster than those asked to simply count sheep. Sleep, by the same token, could be achieved by any number of complex activities that expend mental energy.
An early reference to counting sheep as a means of attaining sleep can be found in Illustrations of Political Economy by Harriet Martineau, from 1832:
"It was a sight of monotony to behold one sheep after another follow the adventurous one, each in turn placing its fore-feet on the breach in the fence, bringing up its hind legs after it, looking around for an instant from the summit, and then making the plunge into the dry ditch, tufted with locks of wool. The process might have been more composing if the field might have been another man's property, or if the flock had making its way out instead of in; but the recollection of the scene of transit served to send the landowner to sleep more than once, when occurring at the end of the train of anxious thoughts which had kept him awake." (355-356)
In India and Pakistan, the phrase synonymous to counting sheep is counting the stars.