ENG-6888. Backport to 4.0.1.x.

Clean up the temp table space when queries finish; Clean up the memory when aggregation finishes