Improve performance of freeMemoryIfNeeded under conditions of heavy dirty keys