Hello, I use pygame to visualize locally on my computer what otherwise can be viewed on my self build led matrix that is driven by a raspberry pi. When I do not use the local display, but the LED matrix, the memory used by the python process stays flat. But when I use the local pygame display - memory usage is steadily increasing. The whole code is on github: https://github.com/stahlfabrik/RibbaPi Here is an example script, that mimics my code and leaks memory as well. I think the code is leaked by pygame - or my usage of pygame.
### #!/usr/bin/env python3 import numpy as np import pygame import sys class Computer(): def __init__(self, width=16, height=16, margin=5, size=30): self.width = width self.height = height self.margin = margin self.size = size self.window_size = (width * size + (width + 1) * margin, height * size + (height + 1) * margin) pygame.init() self.surface = pygame.display.set_mode(self.window_size) pygame.display.set_caption("Leak {}x{}".format(width, height)) self.buffer = np.array([255, 255, 255] * width * height, dtype=np.uint8).reshape(height, width, 3) def show(self, gamma=False): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() self.surface.fill((0, 0, 0)) it = np.nditer([self.buffer[:, :, 0], self.buffer[:, :, 1], self.buffer[:, :, 2]], flags=['multi_index']) while not it.finished: color = (it[0], it[1], it[2]) (row, column) = it.multi_index pygame.draw.rect(self.surface, color, [(self.margin + self.size) * column + self.margin, (self.margin + self.size) * row + self.margin, self.size, self.size]) it.iternext() pygame.display.update() if __name__ == "__main__": import time display = Computer() while True: display.show() time.sleep(1/60) ### OS is mac OS 10.12.2. python3 is from homebrew Python 3.5.2. From pip3 there is numpy==1.11.3 and pygame==1.9.2. Any hint on what goes wrong here is greatly appreciated! Best regards, Chris