Optimise trimMemory method