Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

python - How to implement Garbage Collection in Numpy

I have a file called main.py, which references another file Optimisers.py which only has functions in it and is used in a for loop in main.py. These functions have different optimisation functions in them.

This Optimisers.py then references two other similar files with only functions in them as well, which are in while loops. All of these files use numpy.

I believe that is because of the loops with functions calling on and creating arrays in numpy, which is leading to a memory overload. Therefore I cannot finish some optimisation algorithms, or cycle through all the possible coordinates I would like to.

How do I ensure removal of variables in numpy? As I understand it, numpy's C libraries complicate the standard Python process. What does the %reset array command (from the link below) do? And where should I implement it?

P.S. I've read "Releasing memory of huge numpy array in IPython", and gc.collect() does not work either.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When a numpy array is no longer referenced, it will be automatically freed by the GC. The C objects are wrapped in Python objects, so for you it should not matter how it's implemented.

Make sure that arrays are not referenced in global variables, since those stick around until overwritten or the program exits.

If you need to free an array from a local variable before it goes out of scope you can use del variablename (or just assign e.g. None), but that will not take care of any other references, just the one named.

For debugging where you are referencing an object, you can use gc.get_referrers(object).

P.S. I've read Releasing memory of huge numpy array in IPython and gc.collect() does not work either.

Unless you have cycles or have called gc.disable(), gc.collect() will not make the GC happen sooner.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...