Optimize memory allocation