Matlab out of Memory and cannot be released
Show older comments
Hello, I'm working for my project to periodically read the sensor data through serialport.
The script will read 1sec long data from the sensor and save it to a .mat file every 30 sec.
However, afeter serveral times of saving, the matlab out of memory error occurs and the system memory will not released even Matlab is closed.
I have try the "clear" and "clearAllMemoizedCaches" commands but it seems don't work.
Is this problem caused by calling "save" and "writetable" too many times? Is there any solution to this problem?
The Matlab code is here :
%% start collecting
disp('RUL collecting start');
for i=1:Task_number
close all;
% Read from Uart and do the data preprocess
[errorcode,data_out]=RUL_collect(device,data_length);
%device is the serial port object
clear RUL_collect; %release memory
% get audio data
audio_data=get_audio();% a record object is called in this function
clear get_audio; %release memory
% Save the record data
save_data(data_out,audio_data);% save and write table is called here
clear save_data; %release memory
% show the number of collected smaples
mystring=append('Number of collected: ',string(i));
disp(mystring);
% show the residual memory size
[user,sys] = memory;
mystring=append('Available memory: ',string(user.MemAvailableAllArrays));
disp(mystring);
%Current RUL data collecting complete, wait for next sample
pause(RUL_period);
end
disp('RUL collecting complete');
And the displayed information shows that the available memory decreases each iteration.
RUL collecting start
Number of collected: 1
Available memory: 61703761920
Number of collected: 2
Available memory: 61374980096
Number of collected: 3
Available memory: 61044711424
Accepted Answer
More Answers (1)
Constantino Carlos Reyes-Aldasoro
on 31 May 2023
I think that you are not clearing what is consuming the memory, e.g.
audio_data=get_audio();% a record object is called in this function
clear get_audio; %release memory
When you do this, you will be saving the audio in the variable audio_data, and get_audio is the function that you use to acquire the data, but it is stored in audio_data. You could save your data once acquired, and then clear that variable.
Something similar is happening in the other cases.
1 Comment
ChenPei Yi
on 1 Jun 2023
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!