Skip to content

dask-worker --memory-limit not working? #1015

Closed

Description

@matyasselmeci

Limiting the memory used by a dask-worker using the --memory-limit option seems to have no effect.

Using Python 2.7.5 on a Scientific Linux 7.3 host, with dask 0.14.1 and distributed 1.16.1, I set up a scheduler and a single worker as follows:

dask-scheduler --scheduler-file /tmp/schedfile &
sleep 5
dask-worker --no-nanny --no-bokeh --nprocs 1 --nthreads 1 --memory-limit=250e6 \
    --scheduler-file /tmp/schedfile &

I ran the following client code to test:

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?

Activity

  1. pitrou commented on Apr 12, 2017

    @pitrou
    Member

    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.

    You mean 250MB, right?

  2. matyasselmeci commented on Apr 12, 2017

    @matyasselmeci
    Author

    Yes, my mistake. ps reports memory in K.

  3. mrocklin commented on Apr 12, 2017

    @mrocklin
    Member

    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.

  4. matyasselmeci commented on Apr 12, 2017

    @matyasselmeci
    Author

    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.

  5. mrocklin commented on Apr 12, 2017

    @mrocklin
    Member
  6. matyasselmeci commented on Apr 13, 2017

    @matyasselmeci
    Author

    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.

  7. mrocklin commented on Apr 13, 2017

    @mrocklin
    Member
  8. bluenote10 commented on Jul 6, 2017

    @bluenote10
    Contributor

    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:

    import dask.dataframe as dd
    from dask import delayed
    from dask.distributed import Client, progress
    from distributed.sizeof import getsizeof
    
    client = Client("127.0.0.1:8786")
    
    def simulate_df_partition_load(part_id):
        """
        Creates a random DataFrame of ~1 GB, 100 columns => 10 MB each
        """
        num_rows = 1310000
        num_cols = 100
    
        df = pd.DataFrame()
        for i in xrange(num_cols):
            data_col = np.random.uniform(0, 1, num_rows)
            df["col_{}".format(i)] = data_col
            del data_col    # for max GC-friendliness
    
        print("Partition size: {} MB".format(getsizeof(df) / (2 ** 20)))
        return df
    
    num_partitions = 5
    
    partitions = [
        delayed(simulate_df_partition_load)(part_id)
        for part_id in xrange(num_partitions)
    ]
    df = dd.from_delayed(partitions)
    
    df = df.persist()
    progress(df)
    
    persisted_columns = {}
    for column in df.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.

  9. mrocklin commented on Jul 6, 2017

    @mrocklin
    Member

    How many tasks can be active at once? How many threads does a single worker keep active?

  10. mrocklin commented on Jul 6, 2017

    @mrocklin
    Member

    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.

  11. bluenote10 commented on Jul 6, 2017

    @bluenote10
    Contributor

    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

  12. mrocklin commented on Jul 6, 2017

    @mrocklin
    Member

    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.

  13. bluenote10 commented on Jul 6, 2017

    @bluenote10
    Contributor

    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?

  14. mrocklin commented on Jul 6, 2017

    @mrocklin
    Member

    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.

  15. ogrisel commented on Oct 21, 2017

    @ogrisel
    Contributor

    @matyasselmeci @bluenote10 you might want to try #1488 to see if that solves your issues.

  16. GenevieveBuckley commented on Oct 15, 2021

    @GenevieveBuckley
    Contributor

    @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.

  17. matyasselmeci commented on Oct 15, 2021

    @matyasselmeci
    Author

    Sure, no problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions