2

I want to remove the whole numpy array. For instance, if I make array A like

A = np.zeros((2,3))

I want to remove the whole array A and want to reuse the name 'A' in other purpose.

Are there any codes (functions) to delete the array?

Tim Post
  • 33,371
  • 15
  • 110
  • 174
Philjoon Jung
  • 57
  • 1
  • 2

2 Answers2

3

I want to remove the whole array A

Easy :

del A

want to reuse the name 'A' in other purpose.

Similarly easy :

A = whateverElse  # associate A with whateverElse, so simple :o)
user229044
  • 232,980
  • 40
  • 330
  • 338
user3666197
  • 1
  • 6
  • 50
  • 92
  • 1
    this doesn't free the memory. somehow the array continues to be attached to the memory (even though the variable is not accessible anymore) – Jivan Jun 12 '20 at 00:59
  • 1
    @Jivan - sure, such cases are possible. May test **`A.flags`** before having done the **`del A`** to see more details. Numpy uses smart structures for vector/matrix/tensor data storage & manipulation. Some even do not "own" its own data & are just a ***lightweight-"reader"***-helper into another numpy-object's data, so deleting such non-"owner" will for obvious reasons delete just the ***lightqweight-"reader"-helper***, not the **fat**-*(_foreign by ownership_)*-**data** itself. – user3666197 Jun 12 '20 at 02:26
  • any idea how to free memory from the fat data in these cases, then? – Jivan Jun 12 '20 at 02:52
  • 1
    @Jivan *there is no way to explicitly free memory in Python* – juanpa.arrivillaga Jun 12 '20 at 02:57
  • @juanpa.arrivillaga does it mean that if we create a 50gb array in memory and `del` doesn't free it, then there's no other way to free this memory than killing the process? – Jivan Jun 12 '20 at 03:03
  • @Jivan Yes, essentially. Now, you must understand, that memory may actually be free for the process to use on other python objects... It is a "high water mark". So the object may be freed but your process is still utilizing the memory, from the POV of the operating system. It's also important to understand that `del` doesn't free anything, it deletes a *variable*, which may or may not cause a reference count to reach 0 (which in CPython causes an object to be reclaimed immediately) – juanpa.arrivillaga Jun 12 '20 at 05:49
  • > "_memory may actually be free for the process to use on other python objects_" — i'd rather take your word, but unfortunately this is not what i'm observing — once i allocate a 50gb array, these 50gb are lost forever while the process is running (this is also very annoying, so if you had a solution for this i'd be happy to listen) – Jivan Jun 12 '20 at 08:59
  • FYI - the O/P asked about using the variable name for other purpose, so the association is the subject, not the low-level memory-management ( Python Interpreter, since ever, does not operate any user-facing interface on how to (de)allocate memory for it's object-instances, so remarks about C-language alike free()-ing memory are off-topic here, inside a Python Interpreter context. There is a user-code accessible tool to stimulate or inhibit garbage collection, yet again, it does not meet the O/P indicated problem, the less the context of the said will to re-use the variable name A for other var – user3666197 Mar 31 '22 at 16:12
1

Just assign A to something else. As @juanpa.arrivillaga said, Python will take care of the now "un-referenced" array and do any garbage collection itself.
e.g. A = "new string"

Caders117
  • 319
  • 3
  • 18