0

I am having a problem in python.

Almost all of the programs that I write require "infinite" amounts of Objects,

Rather I should say; an ever increasing amount of Objects!

When I create a list in python to fill it with objects, how come when I type in del(the_list), it doesn't clean up the objects? how can I clean up the objects?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • because `del the_list` simply deletes `the_list` name from whatever namespace you are in. The object refered to by `the_list` will only be garbage collected if it's reference counter goes below zero. – juanpa.arrivillaga Jan 06 '18 at 00:33
  • 3
    Python works by reference counting. Just make sure you don't hold on to other references to old objects which you don't need any longer, and that's all there is to it. – wim Jan 06 '18 at 00:33
  • If the elements aren't referenced elsewhere they *are* deallocated if you delete the list. – Matteo Italia Jan 06 '18 at 00:34
  • 2
    You can force a garbage collection run with `gc.collect` as explained [here](https://stackoverflow.com/q/1316767/289011) but you're not guaranteed that those objects will be actually freed immediately (they actually won't be at all they're still referenced somehow,) As you can see by reading the links provided in that other SO question I linked, garbage collection is not trivial (at all) Also, python might actually mark the memory as "free" but still keep it internally allocated (see [this other thread about it](https://stackoverflow.com/q/15455048/289011) ) – Savir Jan 06 '18 at 00:35
  • 1
    if object doesn't have reference in program then it will be automatically removed from memory by garbage collector. usually you don't need to worry about memory in python – aiven Jan 06 '18 at 00:36
  • How are you determining that the objects aren’t cleaned up? Please add a [mcve]. – Ry- Jan 06 '18 at 03:53

0 Answers0