Improve performance of freeMemoryIfNeeded under conditions of heavy dirty keys
Fix simple
Fix a dead loop situation
Fix a nasty memory leak

Thanks to yi huang for putting me on the right track to finding this.