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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With