Memory management when using map_fn