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:
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);
foregroundmask = zeros(videoSample.Height,videoSample.Width,videoSample.NumFrames);
while hasFrame(videoSample)
frame = readFrame(videoSample);
[inMat,imgInput] = util.createMat(frame);
[outMat,outImg] = util.createMat();
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;
Could some one have any ideal ?

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.
x= (0:0.1:10);
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.

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