There are many theories about why we sleep and dream, and many of these involve memory in some way (including Freud’s theory of why we dream). A relatively recent theory has been gathering support, variously called synaptic renormalisation hypothesis or synaptic homeostasis hypothesis (aka ‘SHY’ – first proposed by Tononi and Corelli, 2003).
The basic principle is the suggestion that sleep provides a necessary opportunity for brain synapses to recover. During the day we are constantly forming new memories – when a new memory is formed essentially the synapse between the neurons involved becomes strengthened. Such strengthening can’t go on forever because of the energy required. So, according to SHY, slow wave sleep provides a slowing down of brain activity in order to allow all synapses to reduce activity. The relative differences in strengths between neurons remains, so the new memories aren’t lost.
Not everyone supports this idea. For example Frank (2013 here) argues that the underlying mechanisms have yet to be clearly defined and, until this happens, the hypothesis remains tentative.
Subscribers to the New Scientist can read about this hypothesis, and more, in a great piece on the Wonder of Slumber (2 February 2013).