Francis Crick, (of Watson and Crick DNA fame), got very interested in brain science toward the end of his life and gave several worldwide lectures on that subject. I was fortunate to attend one of them. During which he proposed several interesting theories. My favorite being that the purpose of dreaming was to identify and collect all the well-separated bits of something designated to be forgot, (tracing the chain of fragmented memory blocks); exactly as you suggest: the biological equivalent of garbage-collection.