I'm trying to understand and optimize the memory usage of Dask. For this simple example, I create an array that I would expect would occupy ~1GB of memory. When I realize the data with persist() this is what I find. But with compute() I see the amount of memory double.
Is this correct? And if so, why?
import dask.array as da
import resource
mem0 = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
memory = lambda: (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss - mem0) / 1024
data = da.random.random((1024, 1024, 128)) # ~1 GB, not yet realized
print(f"Memory used: {memory()} MB")
# Memory used: 0.49609375 MB
data.persist()
print(f"Memory used: {memory()} MB")
# Memory used: 1023.49609375 MB
# Using .compute() instead of .persist()
# Memory used: 2047.5 MB
NB: I did not run persist and compute in the same script. I actually changed the code and ran again, to have a fair comparison.