On Tue, Jan 13, 2015 at 7:54 AM, Rob van der Most <[email protected]> wrote: > Garbage collection in python is actually quite straight forward. It uses ref > counting. When there are no more references to an object, it is directly > released. However if there are reference cycles it uses a limited mechanism > to detect those cycles and clean then up. When there are destructors > involved this mechanism gives up. You can find the cycles it could not clean > up in the gc.garbage list.
Garbage collection is quite straightforward, but memory management is not. Memory is not always released, it might be kept around for future allocations. When I run the test above, gc.garbage comes up empty. When I use gc.getobjects(), I see the number of weakref classes is slowly increasing. There's a cache in Pyglet so you do get an extra weakref for every image ever opened, but weakrefs are tiny. > Note that destructors are advised against in python due to this limitation. > It is better to have some kind of dispose methods you call manually. This limitation was removed in Python 3.4. And in earlier versions, this particular code is not affected . > Rob > > On 13 Jan 2015 05:32, "Luke Miller" <[email protected]> wrote: >> >> So I have the basics of a unit test that loads and unloads a bunch of >> images and force garbage collection, however, it seems that once the >> memory is claimed by the process it is never released. >> >> class PygletTest(unittest.TestCase): >> def test_load(self): >> def get_memory(): >> return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / >> 1000 >> print("Memory at start", get_memory()) >> for fname in glob.glob("data/items/_test_assets/*.png"): >> image = pyglet.image.load(fname) >> print("after loading",fname,get_memory()) >> image = None >> gc.collect() >> print("Memory after setting image to None and calling >> gc.collect()",get_memory()) >> >> Outputs as: >> >> Memory at start: 51.516 >> after loading data/items/_test_assets/background_v2.png 63.104 >> after loading data/items/_test_assets/background.png 69.8 >> after loading data/items/_test_assets/background_v1.png 69.8 >> after loading data/items/_test_assets/background_sky.png 75.288 >> after loading data/items/_test_assets/left.png 75.288 >> after loading data/items/_test_assets/idle1.png 102.216 >> after loading data/items/_test_assets/background_ground.png 102.216 >> after loading data/items/_test_assets/right.png 102.216 >> Memory after setting image to None and calling gc.collect() 102.216 >> >> I understand that garbage collection in python is a bit of a dark art >> but I'd expect memory load to go back down to around 51mb, instead it >> stays at 102mb. >> >> Is the video memory the problem? >> >> >> >> On 7 January 2015 at 12:26, Luke Miller <[email protected]> wrote: >> > Hi, >> > >> > I have a slow memory leak in my pyglet 1.2 program and I have no idea >> > how to debug it. I do a lot of Sprite creation and destruction. The >> > two pyglet classes I use are Sprite and Animation. >> > >> > create: >> > obj._animation = pyglet.image.Animation(frames) >> > obj._sprite = pyglet.sprite.Sprite(obj._animation, **kwargs) >> > >> > destroy: >> > if obj._sprite: >> > obj._sprite.delete() >> > obj._animation = None >> > obj._sprite = None >> > >> > However after several thousand of these loads and unloads the memory >> > use is over 8gb even though inspection of my object list shows only >> > the currently in-use objects (eg five or six objects) with sprites >> > attached. >> > >> > I'm not using Batch and gc.garbage shows an empty list. >> > >> > So yeah, not sure where to start debugging this stuff, any pointers >> > appreciated! >> > >> > Regards, >> > Luke >> >> -- >> You received this message because you are subscribed to the Google Groups >> "pyglet-users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/pyglet-users. >> For more options, visit https://groups.google.com/d/optout. > > -- > You received this message because you are subscribed to the Google Groups > "pyglet-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/pyglet-users. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "pyglet-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/pyglet-users. For more options, visit https://groups.google.com/d/optout.
