0

I'm calling functions as threads and these threads contain lots of data and I want to free up the memory used by them as soon as they're done with it. Here's the general format I'm using:

t = thread.start()
threads.append(t)
for thread in threads:
    thread.join()

Now, the fact that the thread is still referenced by the thread variable. Does that mean all the variables inside it will be preserved as well?

Is there anything special I need to do to free up the memory used by these threads?

e.g.,

del variable_referencing_data
Kern
  • 87
  • 1
  • 5

2 Answers2

2

In Python, you generally don't need to worry about freeing the memory because of Python's Garbage Collector. See this SO question for more information.

Community
  • 1
  • 1
ForceBru
  • 43,482
  • 10
  • 63
  • 98
1

What you refer to as a variable is really a name, which is a way to refer to an object.

The contents of a function are a block in Python. An assignment inside a block binds a name to an object. This scope of this binding is the block.

So as soon as the functions exits, the binding between the names local to the block and the objects they refer to is lost. If these objects do not have other names referring to them, they will be garbage collected.

This is basically why as a rule you shouldn't worry about memory management; Python takes care of this for you.

Trying to load a dataset that is much larger than your computers physical or even virtual memory is an exception to this rule. In this case you'll need to rethink your strategy.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94