Optimizing memory for array