How to clear the memory used in matlab after image processing?
조회 수: 33 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Bhavana Ravirala
2022년 11월 14일
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
Walter Roberson
2022년 11월 15일
clear java
does not remove any java class definitions, and does not remove any memory allocated to the java virtual machine. It does delete some kinds of variables stored at the java level, but it does that by telling Java to delete them, and memory management of the Java virtual machine is up to Java.
Generally speaking, there are several different ways of allocating memory in operating systems. Each request to the operating system to allocate or release memory involves system calls, and those calls can be costly -- not the worst things you can ask for by any means, but if you were to use those calls for every internal requirement for memory, then it would add up. Especially since the operating system calls often can only allocate in multiples of some internal size; for example it might only be able to allocate in multiples of 1Kb or 4Kb (a virtual memory "page")
In the case where you ask the operating system for multiple virtual memory paes and later release the full pages, then the operating system can just add the entire pages back to the list of available pages, a relatively fast operation.
But in the case where you ask for a smaller amount of memory, then the operating system has to somehow keep track of which fragments of a page are available, and it cannot release a virtual memory page back to the operating system for other use until it is satisfied that nothing in the page is in use. The operating system is typically not permitted to move items around in memory because any pointers to the object would be invalidated.
Suppose that you allocate 300 bytes, then another 300 bytes, then a third 300 bytes, and suppose that because it was a relatively small amount, each one was allocated as a fragment of a single memory page. At peak, that was: 300 used for #1, 300 used for #2, 300 used for #3, 124 not allocated
Now delete the first and third 300 bytes, so in the memory page there is now: 300 bytes unused, 300 bytes used #2, (300+124) = 424 bytes unused (the unused 124 combines with the adjacent released 300) bytes. Total unused space in the block: 724 bytes, with the 300 used bytes in the middle. The operating system cannot move the 300 bytes to the beginning of the block because then the program would not know where to look for it.
Now ask to allocate 500 bytes. There is, in theory, enough space for that, because there are 724 unused bytes in the block. But the largest contiguous space in the block is 424 bytes -- so to handle the request, the operating system would have to "grow" the segment to include a second virtual memory page: 300 unused, 300 used, 424 unused, end of block 1024 unused (second block), then merge to 300 unused, 300 used, 1448 unused, allocate the 500 to get 300 unused, 300 used #2, 500 used #3, 948 unused
If you were to then delete the 500 blocks, gettinb back 300 unused / 300 #2 / 1524 unused, can you return the 1524 to the operating system as a whole? No, only complete aligned pages can be returned. Can you divide it back to 300 / 300 / 424 / 1024 and return the second 1024? Possibly, but possibly not, depends on the available calls. How about if you had managed to delete everything used in the first page of a two-VM-page block, could you return the entire block? Possibly, but possibly not. For example if each memory chunk has "housekeeping" information stored at the beginning of a chunk, then there might not be enough room for the housekeeping at the beginning of the second page...
This kind of problem is known as "memory fragmentation", and you can end up with a lot of memory bound up that cannot be returned to the operating system.
When MATLAB is working with sufficiently large data objects, then MATLAB asks the operating system for a single chunk of memory to store the data, knowing that the operating system will need to round the request up to the next multiple of a virtual memory page. This wastes the "ends" of pages, but has the advantage that once MATLAB is done with that data object, it can release the entire chunk of memory back to the operating system, knowing that the operating system will be able to deallocate the chunk from the MATLAB process.
So there are different memory strategies, and not all of them are well-tuned for all purposes. Some of them allow chunks of memory to be returned easily, at the expense of wasting the "end" of the chunk.
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!