1

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

7
  • you can call the garbage collector using cg.collect(). This shouldn't be necessary though, python deals with this internally and if Image is no longer needed it will not use any memory as far as I understand. Commented Feb 15, 2016 at 17:34
  • The unused memory remains unavailable to other programs, though, since most operating systems don't reclaim memory from a process until the process exits. Commented Feb 15, 2016 at 17:35
  • @TobiasR can u please provide exact syntax to do as u have said Commented Feb 15, 2016 at 17:38
  • @chepner how can delete the variable and also deallocate memory Commented Feb 15, 2016 at 17:39
  • 2
    You can't, from Python. You can't give memory back to the operating system. Assuming Image is the only reference to the object, you can simply say del Image to release the memory for use by the Python script itself. Commented Feb 15, 2016 at 17:43

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.