Generalize memory usage for merging to handle larger inputs