Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python garbage collection

I have created some python code which creates an object in a loop, and in every iteration overwrites this object with a new one of the same type. This is done 10.000 times, and Python takes up 7mb of memory every second until my 3gb RAM is used. Does anyone know of a way to remove the objects from memory?

like image 387
utdiscant Avatar asked Jun 23 '09 21:06

utdiscant


People also ask

Does Python have a garbage collection?

The Python garbage collector has three generations in total, and an object moves into an older generation whenever it survives a garbage collection process on its current generation. For each generation, the garbage collector module has a threshold number of objects.

How often does Python garbage collect?

Any time a reference count drops to zero, the object is immediately removed. 295 * deallocated immediately at that time. A full collection is triggered when the number of new objects is greater than 25% of the number of existing objects.

How do you trigger a garbage collection in Python?

There are two ways to perform manual garbage collection: time-based or event-based garbage collection. Time-based garbage collection is pretty simple: the gc. collect() function is called after a fixed time interval. Event-based garbage collection calls the gc.

What is a garbage collector in Python and how does it work?

Introduction to Python garbage collection The Memory Manager destroys the object and reclaims the memory once the reference count of that object reaches zero. However, reference counting doesn't work properly all the time. For example, when you have an object that references itself or two objects reference each other.


1 Answers

I think this is circular reference (though the question isn't explicit about this information.)

One way to solve this problem is to manually invoke garbage collection. When you manually run garbage collector, it will sweep circular referenced objects too.

import gc  for i in xrange(10000):     j = myObj()     processObj(j)     #assuming count reference is not zero but still     #object won't remain usable after the iteration      if !(i%100):         gc.collect() 

Here don't run garbage collector too often because it has its own overhead, e.g. if you run garbage collector in every loop, interpretation will become extremely slow.

like image 161
hasanatkazmi Avatar answered Sep 19 '22 17:09

hasanatkazmi