0

I blit surfaces in my pygame file but the problem is they just stay there. My (hopefully temporary) solution was to just move them far off the window when they aren't in use (i.e. (-5000,0) ), but this decreases performance every time I blit a new surface. Is there any way to delete them? del doesn't seem to do it.

Scott
  • 105
  • 1
  • 8

1 Answers1

1

The memory of a Surface is freed when there is no reference to it anymore, when Python's garbage collection cleans it.

You don't need to use del, just make your program so that you don't refer to unused surfaces anywhere. You can assign None to it if not else.

Sometimes you don't need to keep creating new surfaces all the time but can just use the ones that you already have. But it works fine also to create new and have Python clear the old. I've written some screensaver like Pygame visualizers for VJing and installations way back and have had them running for days, constantly adding new images to the screen and removing old, no memory leaks. And never needed del.

antont
  • 2,676
  • 1
  • 19
  • 21
  • 1
    That's it? Man I feel like it shouldn't be that simple. So, for clarification purposes, if I previously set a surface's position to (-5000,0) I could just replace that by setting the surface to None rather than moving it off screen? – Scott Feb 07 '20 at 22:51
  • 1
    yep. i read from quick googling and my memory says the same. is also how Python and all GC'd languages normally work. just be careful to not have a list of all surfaces or some other place where you'd keep extra references. – antont Feb 07 '20 at 23:24