简体   繁体   English

如何在python中释放ram

[英]How to free ram in python

I have a simple code 我有一个简单的代码

import cv2
import numpy as np

Image = cv2.imread('blue.jpg')

Threshold = np.zero

s(Image.shape, np.uint8) 
cv2.threshold(Image, 121, 255,   cv2.THRESH_BINARY, Threshold) 
cv2.imshow("WindowName", Threshold )

cv2.waitKey(0) 
cv2.destroyAllWindows()

I want to delete matrix 我想删除矩阵

"Image" “图片”

from memeory ie clear memory to save space in ram 从内存即清除内存以节省内存空间

How can i acheive this i am working this code on raspberry pi so 我如何才能做到这一点我正在树莓派上工作此代码

del Image will likely do what you want, but there are some special cases. del Image可能会做您想要的事情,但是有一些特殊情况。

  • It won't do anything if your image is still referenced somewhere else. 如果您的图像仍然在其他地方引用,它将不会执行任何操作。 Reference count has to go to 0. 参考计数必须为0。
  • It won't do anything straight-away if there's a cyclic reference inside Image (it may be freed at some later point, or maybe not at all) 如果Image有一个循环引用,它不会立即做任何事情(它可能在以后释放,或者根本不释放)
  • It will only work this way in cPython. 它只能在cPython中以这种方式工作。 Any other implementation is free to handle garbage collection in a different way. 任何其他实现都可以以其他方式自由处理垃圾收集。 Consider del to be a suggestion. 认为del是一个建议。

You may want to use 您可能要使用

del Image[:]
del Image

Actually , you don't need to do and even don't need to care about it. 实际上 ,您不需要做,甚至不需要关心它。

Python automatically frees all objects that are not referenced any more Python自动释放所有不再引用的对象

Refer this with very quality accepted answer how to release used memory immediately in python list? 用质量非常好的答案引用此内容如何在python列表中立即释放使用的内存?

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

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