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
{{ message }}
Repository navigation
Running out of memory due to task prioritization by scheduler #2470
I am using dask (0.14.3) (Python 3.6.1 :: Anaconda 4.4.0 (64-bit)) and have run into an issue using either the distributed or multiprocessing scheduler on a single machine.
I am applying a function through a map_partitions on a large dask dataframe (read from several h5 files) that returns a result with a small RAM footprint from a relatively large dask partition, and then compute the result.
Doing this, the memory footprint increases until the system runs out of it and the kernel kills a couple of workers. Looking at task progress with the distributed scheduler, a lot of partitions are being read in advance (2-3 per worker), and then wait in RAM for the function to be applied, and thus filling up the RAM unnecessarily.
Is there a way to avoid this suicidal behaviour from the scheduler(s)?
Note: the only way around it I found was reducing the number of workers, which is not ideal.
Also, I wouldn't expect the scheduler to run out of RAM. If you are loading data on workers then the scheduler should never have any data load. The workers will share among each other. Perhaps you're putting data directly into your graph?
Sorry if it was not clear: I am suspecting the workers to fill the RAM, and not the scheduler. All data load is done at the worker level by reading from disk.
Each dask partition is around 50Mo (from pandas.dataframe.info() ), and other input data loaded by the function applied through the map_partition is also around 50 Mo.
I was therefore surprised to see the system run out of RAM with 40 workers on a 40 cores system and 160Go of RAM.
What is the best way to look into workers' data & memory footprint?
The diagnostic web pages show both the expected memory use per worker on the upper left plot of the status/ page and the memory use reported by psutil on the workers/ page
The above workflow should be able to "stream" blocks of 80MB (10e6 float64 per chunk) into the parquet file on the disk. However if I launch a single worker with 1GB of working memory:
distributed.nanny - WARNING - Worker exceeded 95% memory budget. Restarting
distributed.nanny - WARNING - Worker process 52463 was killed by unknown signal
I believe that the scheduler is keeping in memory the intermediate chunks because the df and array variables are still the local scope of the client Python program. I don't know how to tell the scheduler that there is no need to keep this data around in memory (nor even to spill it to disk because we do not need to access df nor array anymore once df has been serialized into the parquet file).
Edit: this is not related to parquet: you can replace the last line by df.mean(axis=0).compute() and you can stil get random worker OOM and restart by nanny.
And it takes up about a GB of RAM when running but no more. This is consistent with having 8 80GB chunks (640MB) + some ongoing or very recent results. Data does not build up over time. 1GB of RAM for a worker is somewhat small, importing numpy, pandas, and others, can take a few hundred MB.
I believe that the scheduler is keeping in memory the intermediate chunks because the df and array variables are still the local scope of the client Python program. I don't know how to tell the scheduler that there is no need to keep this data around in memory (nor even to spill it to disk because we do not need to access df nor array anymore once df has been serialized into the parquet file).
Hum alright. I set the limit to 1GB voluntarily to make it faster to trigger the problem but I think I had seen a similar problem with workers with a much larger limit (but it was also on larger chunks). Maybe it was just bad luck then. The problem is that it's too expensive to reproduce the original failure to investigate.
Also in the example script I gave, I use a single thread, so there is only one single chunk of 80MB of data that is required to be loaded at a time. 1GB should be enough even if several chunks are eagerly loaded by the scheduler ahead of conversion and dumping into the parquet sink.
Maybe open a new issue with your reproduction script and the version numbers of all the libraries. It could be:
a memory leak in pyarrow
a delayed GC of objects with reference cycles
When you have a reference cycle I have the feeling that memory is not immediately freed even when you do an explicit gc.collect(). There can be some delay and this does not seem to be controllable. Maybe @pitrou can shed some light here.
If there is a memory leak in pyarrow, it would be nice to have a reproducer that doesn't involve dask :-)
As for gc.collect(), it should always reciaim dead reference cycles. The only exception is, if another garbage collection is underway (e.g. in a different thread), gc.collect() doesn't do anything.
I would test a few things. Does this also happen with fastparquet? If so
then it probably isn't arrow's fault. Does it also happen with the
threaded or synchronous schedulers? My first guess is that creating and
destroying many Pandas dataframes seems to sometimes leak. I'm not sure
why.
@gabrielelanaro is the memory released once you call del df in the main script? It might just be the case that the intermediate results are kept in workers' memory because you still hold a reference in the driver / client program.
Thanks everyone for your help! I'll further troubleshoot the issue and find out if this indeed related to pyarrow or something else. I'll write more if I find anything.
Activity
mrocklin commentedon Jun 17, 2017
This Stack overflow example may interest you: https://stackoverflow.com/questions/44344171/understanding-memory-behavior-of-dask-distributed
Also, I wouldn't expect the scheduler to run out of RAM. If you are loading data on workers then the scheduler should never have any data load. The workers will share among each other. Perhaps you're putting data directly into your graph?
I recommend producing a minimal example if possible.
olivier-lacroix commentedon Jun 19, 2017
Thank you Matt,
Yes, that looks a lot like it.
Sorry if it was not clear: I am suspecting the workers to fill the RAM, and not the scheduler. All data load is done at the worker level by reading from disk.
Each dask partition is around 50Mo (from pandas.dataframe.info() ), and other input data loaded by the function applied through the
map_partitionis also around 50 Mo.I was therefore surprised to see the system run out of RAM with 40 workers on a 40 cores system and 160Go of RAM.
What is the best way to look into workers' data & memory footprint?
Thanks,
Olivier
mrocklin commentedon Jun 27, 2017
The diagnostic web pages show both the expected memory use per worker on the upper left plot of the status/ page and the memory use reported by
psutilon the workers/ pagemrocklin commentedon Jul 3, 2017
@olivier-lacroix is there anything else to do here or can we close this?
ogrisel commentedon Oct 20, 2017
@mrocklin I have faced what I believed is the same issue. I can reproduce the problem on a minimal example:
The above workflow should be able to "stream" blocks of 80MB (10e6 float64 per chunk) into the parquet file on the disk. However if I launch a single worker with 1GB of working memory:
I get after a minute or so:
I believe that the scheduler is keeping in memory the intermediate chunks because the
dfandarrayvariables are still the local scope of the client Python program. I don't know how to tell the scheduler that there is no need to keep this data around in memory (nor even to spill it to disk because we do not need to accessdfnorarrayanymore oncedfhas been serialized into the parquet file).Edit: this is not related to parquet: you can replace the last line by
df.mean(axis=0).compute()and you can stil get random worker OOM and restart by nanny.ogrisel commentedon Oct 20, 2017
I just found the
compute=Falseoption ofto_parquet. Here is a new version of my code that does not seem to have the afore mentionned problem anymore:EDIT: this is actually wrong. I can also reproduce the memory exhaustion with this version of the code as well.
mrocklin commentedon Oct 20, 2017
On an eight-core machine I run the following:
And it takes up about a GB of RAM when running but no more. This is consistent with having 8 80GB chunks (640MB) + some ongoing or very recent results. Data does not build up over time. 1GB of RAM for a worker is somewhat small, importing numpy, pandas, and others, can take a few hundred MB.
Dask is definitely deleting intermediate values when no longer necessary, at least here on my machine. It might take a second or two for Python's GC to kick in. You can observe this by looking at the progress bars at http://localhost:8787/status . Color on the bar means finished tasks. Slightly more transparent alpha values mean released. I see that only a very small piece of the bar on the right is full color (in memory) while most of the bar is somewhat-transparent (released).
If you were to increase the RAM or decrease the partition size or number of cores I suspect that things would go smoothly.
ogrisel commentedon Oct 20, 2017
Hum alright. I set the limit to 1GB voluntarily to make it faster to trigger the problem but I think I had seen a similar problem with workers with a much larger limit (but it was also on larger chunks). Maybe it was just bad luck then. The problem is that it's too expensive to reproduce the original failure to investigate.
ogrisel commentedon Oct 20, 2017
Also in the example script I gave, I use a single thread, so there is only one single chunk of 80MB of data that is required to be loaded at a time. 1GB should be enough even if several chunks are eagerly loaded by the scheduler ahead of conversion and dumping into the parquet sink.
ogrisel commentedon Oct 20, 2017
But I agree that blaming the variable scoping as I did in #2470 (comment) is wrong as I reproduced the problem with that version of the code as well.
ogrisel commentedon Oct 20, 2017
Aggressively triggering GC when detecting high memory usage in the worker seem to fix the issue when using the 1GB boundary. I will submit a PR.
mrocklin commentedon Dec 19, 2017
I believe that this has been resolved.
gabrielelanaro commentedon Apr 13, 2018
I was recently having this same issue (dask 0.17.2)(distributed 1.21.6(
For some reason the following code shows that the memory of the pyarrow tasks doesn't get cleared. I suspect it has to do with the scheduler.
Is it possible to reopen the issue?
ogrisel commentedon Apr 13, 2018
Maybe open a new issue with your reproduction script and the version numbers of all the libraries. It could be:
When you have a reference cycle I have the feeling that memory is not immediately freed even when you do an explicit
gc.collect(). There can be some delay and this does not seem to be controllable. Maybe @pitrou can shed some light here.pitrou commentedon Apr 13, 2018
If there is a memory leak in pyarrow, it would be nice to have a reproducer that doesn't involve dask :-)
As for
gc.collect(), it should always reciaim dead reference cycles. The only exception is, if another garbage collection is underway (e.g. in a different thread),gc.collect()doesn't do anything.mrocklin commentedon Apr 13, 2018
ogrisel commentedon Apr 13, 2018
@gabrielelanaro is the memory released once you call
del dfin the main script? It might just be the case that the intermediate results are kept in workers' memory because you still hold a reference in the driver / client program.gabrielelanaro commentedon Apr 13, 2018
Thanks everyone for your help! I'll further troubleshoot the issue and find out if this indeed related to pyarrow or something else. I'll write more if I find anything.