Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting variable does not erase its memory from RAM memory

I am using Python (Canopy) extensively for Earth science application. Because my application is memory consuming, I am trying find way to erase variable that I don't need any more in my programs, I tried to use del command to erase the variable memory, but I found that space used by Canopy is still the same. Any ideas about how to erase variable completely from the memory. thanks

like image 311
Kernel Avatar asked Apr 22 '16 13:04

Kernel


People also ask

How do you remove a variable from memory?

Method 1: Delete Variable using del or None from Memory. Method 2: Delete List or Dictionary using del or None from Memory. Method 3: Delete a Function using del or None from Memory. Method 4: Delete User Defined Objects with dir() and globals()

Do variables use RAM?

The relevant part is, yes, they are. For the purposes of a programmer, user, and everything else except the machine itself, all variables and code of your program are stored in RAM.

Which function is used to delete a variable?

The unset() function unsets a variable.

How do you delete a variable?

On Replacement or Execution, right click the variable you want to delete, and select Delete.


1 Answers

You can't manually nuke an object from your memory in Python!

The Python Garbage Collector (GC) will automatically free up memory of objects that have no existing references any more (implementation details differ per interpreter). It's periodically checking for abandoned objects in background without your interaction.

So to get an object recycled, you have to eliminate all references to it by assigning a different value (e.g. None) to all variables that pointed to the object. You can also delete a variable name using the del statement, but as you already noticed, this only deletes the name with the reference, but not the object and its data itself. Only the GC can do that.

like image 81
Byte Commander Avatar answered Sep 22 '22 13:09

Byte Commander