Matlab out of Memory and cannot be released

5 Ansichten (letzte 30 Tage)
ChenPei Yi
ChenPei Yi am 31 Mai 2023
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

Akzeptierte Antwort

ChenPei Yi
ChenPei Yi am 2 Jun. 2023
I find out where the problem is. I should not use the pause function as a timer, it seems to stop every thing matlab is doing include the bacground process. This problem can be solved by change it to
RUL_period=5;% do collection every 30 second
tic;
while toc<=RUL_period
%Matlab donothing
end
And the memory is not decreasing now
RUL collecting start
Memory before save audio: 4388.5548 MB
Memory afeter save audio: 4376.501 MB
Number of collected: 1
Memory before save audio: 4391.1365 MB
Memory afeter save audio: 4379.4457 MB
Number of collected: 2
Memory before save audio: 4390.8809 MB
Memory afeter save audio: 4377.8556 MB

Weitere Antworten (1)

Constantino Carlos Reyes-Aldasoro
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 Kommentar
ChenPei Yi
ChenPei Yi am 1 Jun. 2023
Hello, After checking the available memory step by step, I'm sure that the problem is caused in get_audio. But clearing audio_data immediately after saving still doesn't solve the problem. I wonder if this problem is caused by wrong use of recordobject?
Here is the code of the called function:
% the function save_audio_data() is the saving version of get_audio()
function save_audio_data(Fs,nbit,channel,record_duration)
display_memory('before save audio');
%get audio data from record_Obj
record_Obj=audiorecorder(Fs,nbit,channel);
recordblocking(record_Obj,record_duration);
stop(record_Obj);
%get and save the audio data
audio_data=getaudiodata(record_Obj);
audio_data=array2timetable(audio_data,"SampleRate",Fs);
date=datetime('now','Format','d_MMM_y_HH_m');
str=string(date);
filename=append("machine23_record\audio\",str,"_machine23_audio.mat");
save(filename,'audio_data');
%save complete, clear data
clear audio_data record_Obj;
display_memory('afeter save audio');
end
And the memory are still decreasing calling this function each time
RUL collecting start
Memory before save audio: 26669486080
Memory afeter save audio: 26641891328
Number of collected: 1
Memory before save audio: 26441719808
Memory afeter save audio: 26406461440
Number of collected: 2
Memory before save audio: 26379902976
Memory afeter save audio: 26355322880
Number of collected: 3
Memory before save audio: 26093215744
Memory afeter save audio: 26070786048
Number of collected: 4

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by