Optimize memory management