Java out of memory with serial port

1 view (last 30 days)
Marco
Marco on 1 Oct 2014
Hi, i have a device attached with a serial port connection. Data are sent to the PC with a frequency of 10Hz. In Matlab i have a buffer of 100 rows, the code fills this buffer with the data from the device and at the same time refreshes the plot. When i reach 100, the code saves the data to a file and then overwrites the buffer with new data and so on. When it reaches like 200-300 of these loops, java out of memory error pops out. I tried to force a garbage collection every 100loops with java.lang.Runtime.getRuntime.gc but nothing changed.
I'm running Matlab 2011b and this is the code:
array = zeros(100,5);
array(:,1) = 0:1:99;
h(1) = subplot(2,2,1);
plot(array(:,1),array(:,2),'YDataSource','array(:,2)','XDataSource','array(:,1)');
title('Channel 0');
h(2) = subplot(2,2,2);
plot(array(:,1),array(:,3),'YDataSource','array(:,3)','XDataSource','array(:,1)');
title('Channel 1');
h(3) = subplot(2,2,3);
plot(array(:,1),array(:,4),'YDataSource','array(:,4)','XDataSource','array(:,1)');
title('Channel 2');
h(4) = subplot(2,2,4);
plot(array(:,1),array(:,5),'YDataSource','array(:,5)','XDataSource','array(:,1)');
title('Channel 3');
ii = 1;
jj = 1;
kk=1;
while(1)
value = fgetl(obj1);
array(ii,jj+1) = 4.096/2^24*str2num(value)/7;
refreshdata(h(jj),'caller');
drawnow;
if(jj==4)
jj=1;
ii=ii+1;
else
jj=jj+1;
end
if(ii==101)
ii=1;
save(sprintf('temperature_acq_%d.dat',kk),'array','-ascii','-double');
array(:,1) = array(:,1)+100;
kk = kk+1;
end
end
I don't see any probable memory "leak" or strange allocations. Only thing i have doubts is the sprintf in the save function. Is the memory allocated for the result released after save function completes? Otherwise could be it the problem.
This acquisition must run for days, even months...
Any suggestions?
Thanks

Answers (0)

Community Treasure Hunt

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

Start Hunting!