0

Suppose I have following function(just an example):

a = ['apple', 'mango', 'kiwi']
b = ['apple']

def random_fucnction(a, b):
    len_a = len(a)
    len_b = len(b)

    combined_len = len_a+len_b
    diifrence = list(set(a)-set(b))


    return diifrence, combined_len

in order to reduce memory usually I use del at the end of the function:

def random_fucnction(a, b):
    len_a = len(a)
    len_b = len(b)

    combined_len = len_a+len_b
    diifrence = list(set(a)-set(b))

    del len_a, len_b
    return diifrence, combined_len

is there automatic way finding out unused variable and deleting them inside the function ?

H.Bukhari
  • 1,951
  • 3
  • 10
  • 15
  • 1
    I'm not sure about unused variables but you can force garbage collector after deleting them: `import gc; gc.collect()` – Andrej Kesely Jul 12 '18 at 16:00
  • 2
    I think you need to read up a whole lot more about scope. When the function ends, *all* your local variables are deleted. Also, deleting a variable does not necessarily remove the object it is bound to, just that one particular name. – Mad Physicist Jul 12 '18 at 16:00
  • 1
    [Here](https://rushter.com/blog/python-garbage-collector/) is a good article on garbage collection in python, including use of the `gc` module. – hoffee Jul 12 '18 at 16:01
  • 3
    @AndrejKesely, ...there's not much point to that, though -- they're unreferenced already, so an extra GC call is just adding overhead to try to free up the memory sooner; it'll happen eventually (in a larger, more efficient batch) anyhow. – Charles Duffy Jul 12 '18 at 16:03
  • 2
    Also, in CPython, since these values are not involved in a cycle, `gc.collect()` won’t even clean them up sooner. If they’re already garbage they were already collected; if they weren’t already collected they’re not garbage so it can’t collect them. Even if you have a cycle, what you want to do is manually break it, like `a.bval=None`, and then `a` and `b` will be collected immediately on deletion, with no need to call `collect`. – abarnert Jul 12 '18 at 16:10
  • 1
    @H.Bukhari, ...could you explain why you believe the existing answers don't apply to Python 3 (thus, why reopen is called for)? – Charles Duffy Jul 12 '18 at 16:16
  • I agree with Charles that this doesn’t seem to need a reopen. There’s nothing I can see in my answer here that isn’t covered by the answers there, and if anything they cover _more_ about 3.x (like putting names into a listcomp). But I could be wrong. If there’s anything crucial I covered that they didn’t, or anything that’s still left murky after reading both, please explain what it is so we can improve the answers, whether that means reopening or going over there – abarnert Jul 12 '18 at 16:33

1 Answers1

3

The automatic way is to just not do anything.

Local variables (including parameters) go out of scope at the end of the function. This has the exact same effect as your del, so you’re not doing anything useful.


As for memory usage: Python is automatically garbage collected. If your variable was the only reference to a value, it becomes garbage as soon as the variable is deleted or goes out of scope.

With CPython, collection is primarily done by reference counting. This means the value is destroyed and the memory is returned to the free list as soon as it becomes garbage—unless the value is involved in a reference cycle, in which case it won’t be destroyed until the next run of the cycle collector.

With most other implementations of Python, there’s a more complicated garbage collector that won’t discover the garbage and destroy it until some harder to predict time in the near future.

But either way, del wont change anything. You’re still just removing the variable, which indirectly makes the value garbage and therefore collectible; you’re not doing anything to the value itself.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 1
    @MadPhysicist No it isn’t. The implementation detail is whether that garbage is collected immediately or not, as explained in the answer. It becomes garbage immediately either way. – abarnert Jul 12 '18 at 16:05
  • 1
    Sorry. I misread "garbage" as "garbage collected". You're absolutely right. – Mad Physicist Jul 12 '18 at 16:10