Repository navigation
Aggressively GC on high memory usage in worker - #1488
Conversation
|
I don't think there is an easy way to add a test for this. |
| if frac > min(self.memory_pause_fraction, self.memory_spill_fraction): | ||
| # Force a GC event so as to get more accurate memory usage | ||
| # measurements in case GC is lagging. | ||
| gc.collect() |
There was a problem hiding this comment.
How often is memory_monitor executed? gc.collect can become quite costly if there are many live objects.
There was a problem hiding this comment.
gc.collect is only executed when we are above the high memory fraction. This should not incur any significant performance impact when the memory usage is normal (lower than 70% of the limit by default I think).
There was a problem hiding this comment.
But imagine a worker that's permanently above that fraction. Now you're running the GC over and again (even if it doesn't release any memory)...
There was a problem hiding this comment.
Here is the kind of log I got when running the linked snippet with the 1GB memory limit on the worker with additional logs before and after GC:
I had the code running for 10min or so. So it would not get triggered too often but saved a spurious restart.
There was a problem hiding this comment.
But imagine a worker that's permanently above that fraction. Now you're running the GC over and again (even if it doesn't release any memory)...
The default memory_monitor_interval is 200ms according to the default value of the kwarg. I could add a 5s cool-down period between two consecutive gc calls but that will make the code more complex / hackish.
There was a problem hiding this comment.
I have tried a less aggressive / smarter strategy with extensive logs here: f3981a5 . It kind of works but I still get a random nanny OOM after a while so I am not fully satisfied.
There was a problem hiding this comment.
At least that last option gets rid of the following misleading warning I get most often when running the original snippet on master: "Memory use is high but worker has no data to store to disk. Perhaps some other process leaking memory?"
There was a problem hiding this comment.
Nanny OOMs aren't entirely avoidable if your tasks can quickly generate a moderate fraction of your available memory. It will always be possible that they can conspire to generate enough data to send you over very quickly.
There are costs and benefits to both approaches.
There was a problem hiding this comment.
I will implement the ThrottledGC in this PR. But I am not sure if should better implement the strategy of this PR or the one in f3981a5.
There was a problem hiding this comment.
I don't have any thoughts on which strategy if better. If you think what you have in f3981a5 is nicer then go with that.
|
I have broken the tests by editing the other warning messages. I will push a fix. |
ec2cf46 to
e3f7f59
Compare
|
FYI I'm also tracking down some intermittent failures that have been popping up recently. I haven't seen these before though. |
|
See also #1255 cc @bluenote10 |
|
The failure on travis in |
|
I believe that you can safely ignore Python 2 nanny test failures: #1491 |
|
By "nanny test failures" I mean any test in |
e3f7f59 to
a5dd9cf
Compare
|
I have updated this PR to do less aggressive GC in the worker (only when it has a chance to be beneficial) while also implementing a throttling strategy based on the fraction of the time in GC over the time between 2 consecutive calls. I ran a bunch of long-running tasks that process chunks with a low memory-limit and it seems to work as expected. The frequency of actual GC events depends on the value of the memory-limit but I no longer get the misleading "Perhaps some other process is leaking memory?" warning. I get the "Pausing" / "Resuming" warnings from time to time as expected. I have added debug logs to make it possible to use grep to check that the gc is actually called from time to time (e.g. every couple of seconds in my scripts, while each gc duration is on the order of 100ms). I still get some spurious OOM events by the nanny process but those are much rarer than before on this use case. |
mrocklin
left a comment
There was a problem hiding this comment.
Generally this looks good to me. I have a couple of questions about the parameters. I'm curious if this resolves the problems that @bluenote10 saw earlier.
| throttled_gc.collect() does nothing when repeated calls are so costly and | ||
| so frequent that the thread would spend more than max_in_gc_frac doing GC. | ||
| """ | ||
| def __init__(self, max_in_gc_frac=0.2, warn_if_longer=10, logger=None): |
There was a problem hiding this comment.
Do you have any thoughts on the default value for max_in_gc_frac=0.2? Spending 20% of our time in garbage collection seems unpleasant.
There was a problem hiding this comment.
The warn_if_longer= parameter could also use some mention in the docstring
There was a problem hiding this comment.
Spending 20% of our time in garbage collection seems unpleasant.
It's an upper-bound. In most situations memory-limit is high enough that GC will almost never be called at all.
But in cases where the worker is under heavy memory pressure, I find it better to spend that time in the GC than run the risk to be spuriously killed by nanny for no valid reason. Note that it's not a smoothed average, just based on the last 2 calls to collect.
There was a problem hiding this comment.
I will try to change the fraction to 5% and run some experiments.
There was a problem hiding this comment.
In the meantime, I have pushed an improvement to the docstring as requested. Let me know what you think.
There was a problem hiding this comment.
It's an upper-bound. In most situations memory-limit is high enough that GC will almost never be called at all.
OK, that makes sense to me.
In the meantime, I have pushed an improvement to the docstring as requested. Let me know what you think.
If we're spending 10s in gc then that is probably also something that we should know about. Thoughts on reducing this to 1s?
| memory = proc.memory_info().rss | ||
| if total > need and memory > target: | ||
| # Issue a GC to ensure that the evicted data is actually | ||
| # freed from memory and taken into account by the monitor |
There was a problem hiding this comment.
Is it expected for evicted data to have cyclic references?
There was a problem hiding this comment.
I don't think it's going to be the case for typical workloads but users are free to ship whatever data structures they like into delayed function calls. Why kind of problems do you anticipate?
There was a problem hiding this comment.
I simply think it's bad hygiene to add gc.collect calls "just in case" :-)
There was a problem hiding this comment.
For the record, this is the kind of actual case that was observed by dask-distributed users:
| self.last_collect = collect_start | ||
| self.last_gc_duration = max(time() - collect_start, 1e-6) | ||
| if self.last_gc_duration > self.warn_if_longer: | ||
| self.logger.warning("gc.collect() took %0.3fs. This is usually" |
There was a problem hiding this comment.
Hmm, I'm curious why you think this is useful. Do you know a use case where that warning would have helped?
There was a problem hiding this comment.
To warn datascientists that are inadvertently processing a list of 100 million small dicts at a time in each task instead of processing smaller lists of a couple of million each instead. For instance parsing large JSON files in large chunks instead of small chunks.
I assume that large GC pauses might make the worker non-responsive. But if you think this is useless we can get rid of this warning.
There was a problem hiding this comment.
I assume that large GC pauses might make the worker non-responsive.
It will. But what if using many objects is deliberate and required by the workload? Do we want to warn against any possible programming mistake?
There was a problem hiding this comment.
I think that users would appreciate getting a warning if GC is taking seconds. They have responded well to similar feedback that we have provided in the past (like long event loop pauses).
| to control to log a warning level message whenever an actual call to | ||
| gc.collect() lasts too long. | ||
| """ | ||
| def __init__(self, max_in_gc_frac=0.2, warn_if_longer=10, logger=None): |
There was a problem hiding this comment.
Nitpicking, but allowing 20% of the time inside the GC sounds like a lot.
There was a problem hiding this comment.
Would 5% be a reasonable default?
073f474 to
07010cd
Compare
|
I have reworked this PR to address the comments. I now only trigger throttled GC when it's promising, that is after data eviction. I decided to know longer call the throttled GC after a paused as it's better to call it after eviction, otherwise the throttling mechanism might prevent interesting collections. |
|
Merged. At some point I might want to look at also calling |
This is a possible fix for some of the spurious worker restarts reported in the discussion of dask/dask#2470.
I ran the test script of dask/dask#2470 (comment) with additional logs before and after the call to
gc.collect()and I confirm that many unnecessary restarts can be avoided this way when memory is tight.