简体   繁体   English

Pygame:是否可以卸载图像以节省内存?

[英]Pygame : Is it possible to unload a image for save memory?

I would know if it's possible to unload a image after use. 我想知道是否有可能在使用后卸载图像。 In fact, I load a lot of image for the main, for the settings, the game and creadits too. 实际上,我为主体,设置,游戏和Creadits加载了大量图像。 But there are image useful in a part but completely useless in the three others. 但是,图像在某种程度上是有用的,而在其他三个方面则完全没有用。 I mean for 90%, they are useful in only 1 part. 我的意思是对于90%的人,它们仅在1个部分中有用。 So, I wonder if exist a way to delete the image from the memory of the program, and by the way make it less heavy... 因此,我想知道是否存在一种从程序的内存中删除图像的方法,并因此使其不那么繁重...

The garbage collector will do it automatically when there are not anymore references to the images. 当不再有对图像的引用时,垃圾收集器将自动执行此操作。

For example, say you have myimage = pygame.image.load('path/to/image) . 例如,假设您有myimage = pygame.image.load('path/to/image) If at some point you set myimage = None and you do not have any other reference to that image, the garbage collector will free the memory. 如果在某个时候将myimage = None设置myimage = None并且没有对该图像的任何其他引用,则垃圾收集器将释放内存。
Of course you can also do del myimage . 当然,您也可以做del myimage The del statement will delete the reference immediately (but not the object itself). del语句将立即删除引用(但不会删除对象本身)。

If your game is properly designed (with classes, functions) it's very likely that the garbage collector is already doing its work and you do not need to take care of deleting things. 如果您的游戏经过适当的设计(带有类,函数),则很可能垃圾回收器已经在执行其工作,并且您无需删除所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM