Closed McFlyGold closed 5 years ago
I wanted to elaborate a little bit more on how my images are being loaded into my project. I have numerous sections of code that look like this:
boot1 = Actor('boot1_1') boot1.frame = 1 boot1.active = True
boot2 = Actor('boot2_scroll') boot2.frame = 1 boot2.active = False
boot3 = Actor('boot3_0') boot3.frame = 0 boot3.active = False
And then sections that look like this:
def updateboot1(): if boot1.active: boot1.x = WIDTH/2 boot1.image = "boot1{}".format(boot1.frame) boot1.frame += 1 else: boot1.x = 1000 if boot1.frame > 59: boot1.frame = 1 boot2.active = True boot1.active = False
So when boot1 is active, it centers in the window and displays all the pngs with the boot1 prefix. Once I've exceeded the max number of frames, it shoots off screen (because I couldn't unload them and didn't want them hanging around in the background)_ and starts the next sequence.
If I could unload the boot sequences once I'm done with them, I think that would cut down big time on the memory usage that's happening in my code. (They're only meant to run once anyway) Or is there a better/more effective/more efficient/more practical way to load/display these images? It's not showing up formatted properly. I'm sorry. I've never used github before.
Ok, so I think I found a solution.
It was suggested to me to use "images._cache.clear()" but it didn't work. Something about that bothered me, so after failing a bunch of times, I found that there's a typo there and it should read "images.cache.clear()" instead. There was an underscore that didn't belong there!
After that I was able to add:
def clr_cache():
images.cache.clear()
and then after each of my frame increments, I just added "clr_cache()" and It helped! I was able to reduce the amount of RAM being used up drastically! Even better, it ran on my Pi3 A+ without crashing!
I still have a slowdown issue while loading images, but it's at least running. I don't suppose there's a way to change the FPS at which pgzero is running is there? I figure if I can cut the FPS to 30, I can reduce the number frames I need by half and make the project more manageable for the Pi.
do we want to clear the image surface cache, or unload the image altogether from the loader? if the latter, what happens if draw()
tries to render it?
I'm currently working on a project that uses image sequences as an interface. I have a reddit post about it here with all the complete code in my project: https://www.reddit.com/r/learnpython/comments/almzsn/image_sequence_interface/
The problem seems to be that since there's no way for me to unload images that are not in use anymore, it freezes the system by using up all 512mb of RAM available on the Raspberry Pi Zero and Raspberry Pi3 A+. A script that I ran in my code said it was using up around 738mb of RAM even though the total amount of space for images in my project are 43.9mb!
Mr. Pope suggested to me on twitter to post the issue here and even suggested that I to try images._cache.clear() or del images._cache[name] to remove images from the RAM, but as I'm a complete novice, I couldn't get these to work. I get error messages like "AttributeError: 'Actor' object has no attribute '_cache'"
If there was a way for me to clear a group of loaded images with the same prefix, that would definitely go a long way in helping this project run on the lower end Pis that only have 512mb of RAM.