You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import dask, dask.distributed, dask.array
cli = dask.distributed.Client(scheduler_file='/tmp/schedfile')
x = dask.array.random.random((2000, 2000), chunks=(10, 10))
y = x.T * x
print(cli.gather(cli.compute(y.sum()))
and watched the memory usage via ps -e -o rss,command | grep python | grep -v grep.
I expected the RSS of the dask-worker process to stay below 250K (or just slightly above), but it was around 450K for most of the calculation. With larger array sizes, the RSS rises even more.
Is this a bug? If not, what is the best way to guarantee that a dask-worker does not go over a hard limit, memory-wise?
I can't immediately explain why you would be seeing significantly more memory use, however I should note that the --memory-limit mechanism isn't a hard limit. Instead it is a limit at which Dask starts to push data to disk. It doesn't track the process at all, it only tracks the data that it keeps in the worker's data storage dictionary. When the estimated number of bytes in this dictionary exceeds the limit then it starts writing to the filesystem.
So many things could be going on here, maybe the executing functions are using lots of memory. Maybe the filesystem is using memory (though I wouldn't expect this to be assigned to the process). Maybe Python isn't freeing memory quickly after garbage collection.
If you want a hard limit on the process you'll probably be better served using operating system utilities like cgroups.
When Dask pushes data to disk, does it write to the /tmp/worker-XXXXXX/storage directory it creates? I was watching it while running the test, but I didn't see any files.
I understand that --memory-limit only limits the worker's storage and the rest of the process uses more than that, but I figured that the overhead would be more or less constant as long as I'm only using Dask's data structures and methods, and I could account for it.
Fascinating! So is doing sum(dask_worker.nbytes.values()) a good way of getting the number of bytes of data in the worker? It peaked at around 20 MB during my tests, so no wonder nothing got sent to disk.
Yeah. That value is an estimate though. It's good for numpy arrays and
pandas dataframes, but not always the best for arbitrary objects. See
distributed/sizeof.py
The softness of the memory limit also emerges as major issue in our experiments. For example: When running with 16 GB worker nodes, we see workers going out-of-memory even if we set the memory limit as low as 2 GB due to occasional memory peaks -- which on the other hand means that we are wasting >80% of the workers memory most of the time. We are currently trying to understand where these peaks are coming from and if there is anything to prevent them. The docs mention three possible reasons:
Sizeof: We are using only DataFrames and Series of non-object dtypes (floats and ints). Is it correct to assume that the sizeof operator works in these cases?
Relative memory limit: We are setting the memory limit absolutely e.g. to 2 GB, so this is should apply.
This leaves: Computations and communications may take up additional RAM not accounted for. How can we debug and quantify this?
I was trying to come up with a minimal example which exceeds the memory limit significantly. I couldn't quite reach the factor >8 we see in the larger experiments, but the following example reaches about 8 GB when running with a single worker (single-threaded) of 2 GB limit:
importdask.dataframeasddfromdaskimportdelayedfromdask.distributedimportClient, progressfromdistributed.sizeofimportgetsizeofclient=Client("127.0.0.1:8786")
defsimulate_df_partition_load(part_id):
""" Creates a random DataFrame of ~1 GB, 100 columns => 10 MB each """num_rows=1310000num_cols=100df=pd.DataFrame()
foriinxrange(num_cols):
data_col=np.random.uniform(0, 1, num_rows)
df["col_{}".format(i)] =data_coldeldata_col# for max GC-friendlinessprint("Partition size: {} MB".format(getsizeof(df) / (2**20)))
returndfnum_partitions=5partitions= [
delayed(simulate_df_partition_load)(part_id)
forpart_idinxrange(num_partitions)
]
df=dd.from_delayed(partitions)
df=df.persist()
progress(df)
persisted_columns= {}
forcolumnindf.columns:
persisted_columns[column] =df[column].persist()
The partitions have a size of 1 GB each. In theory exceeding the limit of 2 GB should not be necessary, because the get_item operation has to perform a read-from-disk of a 1 GB partition, producing a 10 MB output, which could be written away immediately.
To be explicit, if you have eight threads on your worker and each thread starts up one of these tasks then I can definitely imagine the process shooting up to 8GB. If this is the case then I would recommend looking at other options, like cgroups or containerization.
We could shut-down processing if we identify a case like this, but we can't do this in a fool-proof manner. It's hard/impossible to limit what people can do in their tasks.
We always force the workers to be single threaded to avoid that. Typically the command line args include --nthreads 1 --nprocs 1 --memory-limit 2000000000 --resources MEM=2000000000
You could look at worker.executing to verify that its length is at most 1. You could look at the size of all objects in worker.data.fast to ensure that their sum stays below the 2GB limit. Beyond that I'm not sure, I would probably start looking at normal Python mechanisms to track down memory issues.
The output of worker.executing and worker.data.fast looks reasonable, i.e., no parallelism and the LRU stays within its limits. However I'm observing situations where worker.data.fast is empty <LRU: 0/100000000 on dict>, but still the worker holds on to several GB of data. Do you think it is possible that this line leads to a leak or is it maybe the callback that may keep references to the data?
As far as I know there aren't any callbacks registered (although this is something that you could inspect. Generally I don't see an opportunity for a leak there. I could easily be missing something though.
@matyasselmeci I hope the information above has been enough to answer your question. If you have more questions, feel free to reopen this issue or start a new issue.
Activity
pitrou commentedon Apr 12, 2017
You mean 250MB, right?
matyasselmeci commentedon Apr 12, 2017
Yes, my mistake.
psreports memory in K.mrocklin commentedon Apr 12, 2017
I can't immediately explain why you would be seeing significantly more memory use, however I should note that the
--memory-limitmechanism isn't a hard limit. Instead it is a limit at which Dask starts to push data to disk. It doesn't track the process at all, it only tracks the data that it keeps in the worker's data storage dictionary. When the estimated number of bytes in this dictionary exceeds the limit then it starts writing to the filesystem.So many things could be going on here, maybe the executing functions are using lots of memory. Maybe the filesystem is using memory (though I wouldn't expect this to be assigned to the process). Maybe Python isn't freeing memory quickly after garbage collection.
If you want a hard limit on the process you'll probably be better served using operating system utilities like cgroups.
matyasselmeci commentedon Apr 12, 2017
When Dask pushes data to disk, does it write to the
/tmp/worker-XXXXXX/storagedirectory it creates? I was watching it while running the test, but I didn't see any files.I understand that
--memory-limitonly limits the worker's storage and the rest of the process uses more than that, but I figured that the overhead would be more or less constant as long as I'm only using Dask's data structures and methods, and I could account for it.mrocklin commentedon Apr 12, 2017
Yes, it writes there. If you feel adventurous you might investigate the worker itself using one of the following methods:
For example:
matyasselmeci commentedon Apr 13, 2017
Fascinating! So is doing
sum(dask_worker.nbytes.values())a good way of getting the number of bytes of data in the worker? It peaked at around 20 MB during my tests, so no wonder nothing got sent to disk.mrocklin commentedon Apr 13, 2017
bluenote10 commentedon Jul 6, 2017
The softness of the memory limit also emerges as major issue in our experiments. For example: When running with 16 GB worker nodes, we see workers going out-of-memory even if we set the memory limit as low as 2 GB due to occasional memory peaks -- which on the other hand means that we are wasting >80% of the workers memory most of the time. We are currently trying to understand where these peaks are coming from and if there is anything to prevent them. The docs mention three possible reasons:
I was trying to come up with a minimal example which exceeds the memory limit significantly. I couldn't quite reach the factor >8 we see in the larger experiments, but the following example reaches about 8 GB when running with a single worker (single-threaded) of 2 GB limit:
The partitions have a size of 1 GB each. In theory exceeding the limit of 2 GB should not be necessary, because the
get_itemoperation has to perform a read-from-disk of a 1 GB partition, producing a 10 MB output, which could be written away immediately.mrocklin commentedon Jul 6, 2017
How many tasks can be active at once? How many threads does a single worker keep active?
mrocklin commentedon Jul 6, 2017
To be explicit, if you have eight threads on your worker and each thread starts up one of these tasks then I can definitely imagine the process shooting up to 8GB. If this is the case then I would recommend looking at other options, like cgroups or containerization.
We could shut-down processing if we identify a case like this, but we can't do this in a fool-proof manner. It's hard/impossible to limit what people can do in their tasks.
bluenote10 commentedon Jul 6, 2017
We always force the workers to be single threaded to avoid that. Typically the command line args include
--nthreads 1 --nprocs 1 --memory-limit 2000000000 --resources MEM=2000000000mrocklin commentedon Jul 6, 2017
You could look at
worker.executingto verify that its length is at most 1. You could look at the size of all objects inworker.data.fastto ensure that their sum stays below the 2GB limit. Beyond that I'm not sure, I would probably start looking at normal Python mechanisms to track down memory issues.bluenote10 commentedon Jul 6, 2017
The output of
worker.executingandworker.data.fastlooks reasonable, i.e., no parallelism and the LRU stays within its limits. However I'm observing situations whereworker.data.fastis empty<LRU: 0/100000000 on dict>, but still the worker holds on to several GB of data. Do you think it is possible that this line leads to a leak or is it maybe the callback that may keep references to the data?mrocklin commentedon Jul 6, 2017
As far as I know there aren't any callbacks registered (although this is something that you could inspect. Generally I don't see an opportunity for a leak there. I could easily be missing something though.
ogrisel commentedon Oct 21, 2017
@matyasselmeci @bluenote10 you might want to try #1488 to see if that solves your issues.
GenevieveBuckley commentedon Oct 15, 2021
@matyasselmeci I hope the information above has been enough to answer your question. If you have more questions, feel free to reopen this issue or start a new issue.
matyasselmeci commentedon Oct 15, 2021
Sure, no problem.