On Sep 17, 6:31 am, Chris Rowson <[email protected]> wrote: > Hi folks, > > I'm trying to get my head around what happens to data stored in memory when > it isn't needed anymore. > > Let me give you an example. Let's say I create a function which returns a > dict called 'results' populated with data from an external source each time > a user registers (for instance let's say it has data returned from geocoding > the users postcode and providing info about the area). > > So the 'results' dict holds that data, it is used, and then what? Is the > memory automatically freed up again when we finish with the data or does it > hang around in memory?
Yes. Python uses reference counting. Every variable is a pointer to a memory location. It keeps tracks of how many pointers point to the same location. a = A() # one pointer b = a # two pointers c = a # three pointers when you delete one the counter is decreased b = 4 # now two variables (a,c) point to A() when the counter is zero the object is freed. This works great UNLESS the objects have circular references (a.x=a) AND the object has a destructor (A.__del__). As long a you do not use destructors (who needs them?) everything works great.

