How to clear the memory used in matlab after image processing?

23 views (last 30 days)
Dear All,
I try to find the way to release or clear memory of matlab after finish image processing. But I fail even use clear all or clearvar--.
Bellow is an example I copy it from matlab website:
https://www.mathworks.com/help/releases/R2021b/vision/ug/subtract-image-background-using-opencv-code-in-matlab.html
videoSample = VideoReader("atrium.mp4");
videoSample.CurrentTime = 2.5;
import clib.opencv.*;
import vision.opencv.*;
history = 300;
threshold = 400;
shadow = true;
cvPtr = cv.createBackgroundSubtractorKNN(history,threshold,shadow);
kNNBase = util.getBasePtr(cvPtr);
kNNBase.setkNNSamples(2);
foregroundmask = zeros(videoSample.Height,videoSample.Width,videoSample.NumFrames);
while hasFrame(videoSample)
frame = readFrame(videoSample);
[inMat,imgInput] = util.createMat(frame);
[outMat,outImg] = util.createMat();
kNNBase.apply(imgInput,outImg);
foregroundmask = util.getImage(outImg);
foregroundmask = rescale(foregroundmask);
foregroundmask = cast(foregroundmask,"like",frame);
foreground(:,:,1) = frame(:,:,1).*foregroundmask;
foreground(:,:,2) = frame(:,:,2).*foregroundmask;
foreground(:,:,3) = frame(:,:,3).*foregroundmask;
image(foreground,Parent=gca);
pause(0.01);
end
Could some one have any ideal ?
Thanks

Answers (1)

Bhavana Ravirala
Bhavana Ravirala on 14 Nov 2022
MATLAB will not completely delete a set of data until all the references to the data have been removed.
Example:
x= (0:0.1:10);
y=-x;
p=plot(x,y);
clear all;
With the help of “clear all command “x” and “y” variables will be deleted but MATLAB makes a copy of x-and y- data necessary for the plot since the figure has not been closed.
For this copied data to be deleted, the line object created by the plot command must be deleted or the figure need to be closed which can be done by “close all” command.
close all;
Since the line object within the figure was the only object referencing the copied x- and y- data, MATLAB automatically frees the memory being used since there are no more active references to the data.
  10 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by