Optimize memory usage and allocation