简体   繁体   中英

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.

  • It won't do anything if your image is still referenced somewhere else. Reference count has to go to 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)
  • It will only work this way in cPython. Any other implementation is free to handle garbage collection in a different way. Consider del to be a suggestion.

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

Refer this with very quality accepted answer how to release used memory immediately in python list?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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