2

Could you please let me know how python removes unwanted data running in a python process.

Will that be deleted automatically when python process terminated?

I have a server process which will have to run for months, then in that case will it keep the memory for that unwanted data?

user2225190
  • 547
  • 1
  • 6
  • 18
  • 1
    Yes. Once the Python process is stopped, all the memory consumed are released. There is an automatic garbage collector that also runs, that will clear out unused variables. However, it is your job to plan the data you keep in the memory, at the time of design. – thiruvenkadam Dec 21 '16 at 09:29
  • What about processes running for years, will it keep that data for long time – user2225190 Dec 21 '16 at 09:52
  • 1
    Data will be in the memory and not deleted until the end of life cycle of the process. Provided, the data being used(or referenced in the current scope of the process) by the process. Python's GC will take care of the local variables created but no longer used even when the process is running. – thiruvenkadam Dec 21 '16 at 12:34

1 Answers1

5

Yes, Python garbage collector removes every object not referenced to. The feature is based on reference counting. However it can also deal with cyclic references.

Of course when the process is terminated, all its resources are released. However it is rather OS dependent.

[EDIT]

In case of long-living processes, there are at least two possibilities of a memory leak:

  • the process may keep references to objects permanently,
  • objects with finalizers (.__del__() method) may get stuck in a noncollectable cycle (see http://arctrix.com/nas/python/gc/ for details).
abukaj
  • 2,582
  • 1
  • 22
  • 45