don joe
don joe

Reputation: 41

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 i.e clear memory to save space in ram

How can i acheive this i am working this code on raspberry pi so

Upvotes: 1

Views: 5301

Answers (2)

Van Tr
Van Tr

Reputation: 6121

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?

Upvotes: 0

viraptor
viraptor

Reputation: 34205

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.

Upvotes: 1

Related Questions