Reading in large 4mb files from SD Card - G120

All,

I’m trying to read a file that is roughly 4MB in size into an byte[] array with the G120. I know the G120 has 13MB of sram, so I should have no problems there?

Here is my code, i’m using .netmf 4.2 (since that’s what Tinkr is built for)


PersistentStorage sdCard = new PersistentStorage("SD");
sdCard.MountFileSystem();

string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            
FileStream fs = new FileStream(rootDirectory+"\\test.bin", FileMode.Open);
long fileSize = fs.Length;

byte[] data = new byte[fileSize];

int readCount = fs.Read(data, 0, data.Length);

sdCard.UnmountFileSystem();


It blows up when it tries to create the byte array to specified length.


GC: performing heap compaction...
    #### Exception System.OutOfMemoryException - CLR_E_OUT_OF_MEMORY (1) ####
    #### Message: 
    #### GHI_SDCardTesting.Program::Main [IP: 007a] ####
A first chance exception of type 'System.OutOfMemoryException' occurred in GHI_SDCardTesting.exe

How should I handle wanting to work with data this big?

Thanks in advance!

@ zeroaviation -
try Lagre Buffer if 4.3 and earlier.
https://www.ghielectronics.com/docs/58/large-memory-objects-and-bitmaps

@ Dat - Thanks for the reply, unfortunately that did not work

Here is the code modified

 
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream fs = new FileStream(rootDirectory+"\\tune.bin", FileMode.Open);
            long fileSize = fs.Length;

            Stopwatch sw = Stopwatch.StartNew();

            using (LargeBuffer lb = new LargeBuffer((int)fileSize))
            {
                int readCount = fs.Read(lb.Bytes, 0, lb.Bytes.Length);

                sw.Stop();

                Debugger.Break();
            }
            
            sdCard.UnmountFileSystem();

The array in the LargeBuffer object returns 0x00’s for the entire size.

Is that the proper way to utilize it? Also, that bit of code took 13~ seconds to run, (even though it got no result). Can anything be done to speed it up?

Thanks again

@ zeroaviation -

[quote]The array in the LargeBuffer object returns 0x00’s for the entire size.
[/quote]
That because fileSize was zero, not LargeBuffer

1 Like

Thanks for the reply Dat. I figured it out, ended up with two files on the SD card that were not correct.

Anything that can be done to speed up the read time? 13 seconds is pretty lengthy for only 4mb (I know embedded and .netmf is slow…)

Thanks again!