Clearing memory on specific variables to keep performance

Ever wondered if you can use a function than the garbage collector to remove unwanted variables on your script? You know, sometimes you just work on huge data structures, and it keeps being on the memory causing starvation on memory for the rest of the script. Also, sometimes you don’t need anything but a few variables to keep working on for the rest of your code.

Isn’t this what the del <variable name> statement does?

I think you want:

import gc

gc.collect()

I know that garbage collector functions to clear memory except that it unfortunately doesn’t permanently delete variables from memory and can be used later, I meant something that could be used to permanently remove a variable from both memory and interpreter

That’s exactly what it does, but it can’t be used to exclude variables explicitly, that’s why I suggested a function to be used on these criteria too.

It would be bad for memory safety if you could deallocate objects that you still keep references to.

If del isn’t freeing memory for you, then you probably kept a reference somewhere that you didn’t realise.