How to optimize the memory usage of "linking"?