As someone who suffers from insomnia on a regular basis I have theorized that since the brain sucks in every bit of input you experience. Every smell, sound, thought, sight, touch, whatever. All that input just sits in your brains RAM until you give the brain time to download and process the information. This is why the body gets the same amount of regenerative energy from just 15 minutes of calming downtime but you still have a need for sleep.
Thats what dreams are. Part of your brain is built to make sense outta input so part of your brain watches this info flying by as its being put away and starts making stories up from it. Why? bored maybe? maybe thats its job and it doesnt know how NOT to do its job so since it cant use current input it uses what it has available.
Anyway once your RAM gets full then the machine starts to break down. It is full to bursting with all of this data and hasnt had any down time to process it. The computer starts to error out. Your brain bluescreens. Too many bluescreens and the whole thing just shuts down.
Anyway thats been my theory for a while but what do I know. Just had 3 decades of sleepless nights to contemplate it.