memory leaks - Pygame surfaces less than 256 by 256 pixel don't seem to be collected by the GC (Windows 11, python 3.11.

When I run this script, the task manager shows that when the cache list if full the program uses around

When I run this script, the task manager shows that when the cache list if full the program uses around 2GB and when popping all elements it goes down to about 20MB.

But, if the surfaces are 255 by 255 instead of 256 by 256 for exemple, it first goes up to 2GB like before, but when emptying the list it stays at 2GB.

import pygame
from pygame import Surface

pygame.init()
pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()

nb = 10000

running = True
while running:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False
        elif e.type == pygame.KEYDOWN:
            if e.key == pygame.K_SPACE and cache:
                for _ in range(nb):
                    cache.pop()
                print("cleaned")
            elif e.key == pygame.K_g:
                cache = [Surface((256, 256)).convert() for _ in range(nb)]
    
    clock.tick(60)

I also tried to use Pympler to get the memory usage by putting this line just after the one that fills up cache: summary.print_(summary.summarize(muppy.get_objects())), it correctly displayed that there were 10000 surfaces but their combined memory usage was only 625KB:

                       types |   # objects |   total size
============================ | =========== | ============
                        code |        3729 |      1.36 MB
                         str |       12611 |      1.35 MB
                        dict |        2315 |    881.16 KB
                        type |         790 |    751.08 KB
      pygame.surface.Surface |       10000 |    625.00 KB
                       tuple |        2504 |    145.08 KB
                        list |         399 |    138.80 KB
                         set |          93 |    119.62 KB
          wrapper_descriptor |        1429 |    100.48 KB
  builtin_function_or_method |        1287 |     90.49 KB
                 abc.ABCMeta |          70 |     86.89 KB
           method_descriptor |        1172 |     82.41 KB
       weakref.ReferenceType |         992 |     77.50 KB
                         int |        1841 |     55.63 KB
                   frozenset |         113 |     46.09 KB

Any ideas as to what could be happening?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744315703a4568171.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信