Dynamic memory allocation in python

I created a large multidimensional array M with np.zeros((1000,1000)) . After a certain number of operations, I no longer need it. How can I free RAM dynamically during program execution? Does this do M=0 for me?

+6
source share
3 answers

In general, you canโ€™t. Even if you delete all references to the object, the python implementation still has the option of reusing or freeing memory. On CPython, you can call gc.collect() to force garbage collection to start. But while this may return the memory, it does not necessarily return it to the OS.

But : numpy is an extension module that does its own work and manages its own memory.

When I track memory usage in a python process, I see that RAM usage (Resident Set Size) decreases after del(M)

 In [1]: import numpy as np In [2]: M = np.zeros((1000,1000)) In [3]: del(M) In [4]: 

Immediately after starting IPython:

 slackbox:~> ps -u 77778 USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND rsmith 77778 0.0 0.5 119644 22692 0 S+ 2:37PM 0:00.39 /usr/local/bin/py 

After importing numpy (1):

 slackbox:~> ps -u 77778 USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND rsmith 77778 1.0 0.8 168548 32420 0 S+ 2:37PM 0:00.49 /usr/local/bin/py 

After creating the array (2):

 slackbox:~> ps -u 77778 USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND rsmith 77778 0.0 1.0 176740 40328 0 S+ 2:37PM 0:00.50 /usr/local/bin/py 

After calling del (3):

 slackbox:~> ps -u 77778 USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND rsmith 77778 0.0 0.8 168548 32496 0 S+ 2:37PM 0:00.50 /usr/local/bin/py slackbox:~> 

Therefore, in this case, using del() can reduce the amount of RAM used.

Note that there is an exception to this with numpy. Numpy can use memory allocated by another extension library. In this case, the numpy object is marked as numpy does not own memory, and the release remains in another library.

+12
source

Use the del operator:

 del M 

And by the way, an array of form float64 (1000, 1000) takes up only 7 MB. If you have memory problems, the problem is probably somewhere else.

+3
source

There are two ways ........

1). del M

  But it will delete the array object it self. 

2). M.clear ()

  you can clear the array without deleting M object 
0
source

Source: https://habr.com/ru/post/943963/


All Articles