Reading uint8 from raw data takes much more RAM then expected

I am trying to read some MB of data from big files of several GB. I am using the follwing code:
fid=fopen(filename,'r');
fseek(fid,someStartValue,'bof');
raw = fread(fid, [3 ceil(numOfPixels/2)*numOfFrames],'uint8','b');
number of uint8 are
3*ceil(numOfPixels/2)*numOfFrames=184688640
so this is also the value in bytes, corresponding to about 185MB. But when calling the fread line, the taken RAM increases by 1.6 GB.
Any ideas?
Running the fread line mutiple times in a row gives the peaks shown in the attached image. I also don't understand, why there is more RAM available after the peak compared to before the peak. The image shows three calls of the line

댓글 수: 4

Hello Jonas, did you chek this:
https://www.mathworks.com/help/matlab/matlab_prog/strategies-for-efficient-use-of-memory.html
Jonas
Jonas 2023년 1월 18일
편집: Jonas 2023년 1월 18일
thanks for the link. Guessing fread internally stores those values as double first, the size factor is 8 between uint8 and double which would equal about 1.5 GB. sounds like that could be the problem. tomorrow i will try
raw = fread(fid, [3 ceil(numOfPixels/2)*numOfFrames],'uint8=>uint8','b');
Les Beckham
Les Beckham 2023년 1월 18일
편집: Les Beckham 2023년 1월 18일
That should fix your issue. If you specify the precision argument as 'uint8' as your initial post shows, it will take the uint8 source data and put it into doubles. Using 'uint8=>uint8' will keep the data as uint8, reducing the memory usage by a factor of 8. As Image Analyst suggests in his answer, use the whos command to check the data type and memory size of your raw array after you read it in .
issue solved, thanks.

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2023년 1월 18일
Put the memory command before and after your code. What do you see? Are you sure you're closing the file?
memory
fid=fopen(filename,'r');
fseek(fid,someStartValue,'bof');
raw = fread(fid, [3 ceil(numOfPixels/2)*numOfFrames],'uint8','b');
fclose(fid);
whos raw
memory

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2022a

태그

질문:

2023년 1월 18일

댓글:

2023년 1월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by