3

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?

6
  • 2
    Python is a memory-managed language, and objects are automatically reclaimed once their reference count reaches zero. Commented May 11, 2018 at 6:21
  • @juanpa.arrivillaga ... except numpy is not really python and doesn't necessarily work that way Commented Jun 12, 2020 at 2:54
  • @Jivan yes, it is. And yes, it does. Commented Jun 12, 2020 at 2:55
  • well, try del my_numpy_array_taking_50gb_in_memory and see what happens in htop then — memory is still allocated Commented Jun 12, 2020 at 2:57
  • @Jivan read the following: stackoverflow.com/questions/15455048/releasing-memory-in-python Commented Jun 12, 2020 at 5:50

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

8 Comments

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 - 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.
any idea how to free memory from the fat data in these cases, then?
@Jivan there is no way to explicitly free memory in Python
@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?
|
0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.