Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Leak when using np.random.choice with replace = False #14169

Closed
GrimLefourbe opened this issue Jul 31, 2019 · 4 comments
Closed

Memory Leak when using np.random.choice with replace = False #14169

GrimLefourbe opened this issue Jul 31, 2019 · 4 comments

Comments

@GrimLefourbe
Copy link

GrimLefourbe commented Jul 31, 2019

When using choice with an integer arg, the choice function creates a randomised array with np.permutations then returns a slice of this array. This keeps the big array in memory instead of just size elements. The choice function should return a copy of the slice to decrement the ref count of the temporary array.

import numpy as np
a = np.random.choice(a=10**8, size=100, replace=False)

Numpy/Python version information:

Tested in 1.16 but I did not find a fix in the master.

[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.10.44.4)]```
@seberg
Copy link
Member

seberg commented Jul 31, 2019

In case you are interested in that, but np.random.default_rng.choice would have a much better behaviour here. (The new random number generator API, it is worth a look!).

@charris
Copy link
Member

charris commented May 28, 2020

Is this still a problem?

@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label May 28, 2020
@charris charris added this to the 1.20.0 release milestone May 28, 2020
@WarrenWeckesser
Copy link
Member

@charris, yes, for the legacy random interface, it still is:

idx = self.permutation(pop_size)[:size]

The recommended way to avoid the problem is to do what @seberg suggested in a comment last year: use the new API that was introduced in 1.17. It doesn't have this problem:

In [8]: rng = np.random.default_rng()

In [9]: a = rng.choice(10**8, size=100, replace=False)

In theory, we could fix the old code by making a copy after slicing, but at this point, I don't think we should bother. If it isn't crashing Python, our policy has been to not touch the old code.

@mattip mattip removed this from the 1.20.0 release milestone May 31, 2020
@mattip
Copy link
Member

mattip commented May 31, 2020

Closing as a "won't fix" and removing the milestone. Please undo if I am mistaken.

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

No branches or pull requests

5 participants