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

don joe
  • 41
  • 1
  • 6
  • 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. – Tobias Feb 15 '16 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. – chepner Feb 15 '16 at 17:35
  • @TobiasR can u please provide exact syntax to do as u have said – don joe Feb 15 '16 at 17:38
  • @chepner how can delete the variable and also deallocate memory – don joe Feb 15 '16 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. – chepner Feb 15 '16 at 17:43
  • so by using del Image frees the portion where Image is stored , to be used by other program ??? – don joe Feb 16 '16 at 00:58
  • `del Image` frees the memory for Python to use for other things, but Python may or may not return the memory to the OS for other programs to use. – strubbly Feb 17 '16 at 08:36

2 Answers2

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.
viraptor
  • 33,322
  • 10
  • 107
  • 191
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?

Community
  • 1
  • 1
Van Tr
  • 5,889
  • 2
  • 20
  • 44