SecureStorage read/write

I have this for reading data from SecureStorage.
How can I improve the read method to only load actual data, array size is variable?
Or can I dispose of the buffer? Because on second read I run into System.OutOfMemory

class Storage
    {
        private SecureStorageController configurationStorage;

        public Storage()
        {
            configurationStorage = new SecureStorageController(SecureStorage.Configuration);
        }

        public void Erase() => configurationStorage.Erase();

        public void Write(byte[] writeBuffer)
        {
            Erase();

            int blocks = (int)Math.Ceiling((double)writeBuffer.Length / 32);

            for (int i = 0; i < blocks; i++)
            {
                int copySize = writeBuffer.Length - (i * 32);
                byte[] block = new byte[32];
                Array.Copy(writeBuffer, i * 32, block, 0, (copySize > 32) ? 32 : copySize);
                configurationStorage.Write((uint)i, block);
            }
        }

        public byte[] Read()
        {
            byte[] readBuffer = new byte[131072];

            for (int i = 0; i < 4096; i++)
            {
                byte[] readBlock = new byte[32];
                configurationStorage.Read((uint)i, readBlock);
                Array.Copy(readBlock, 0, readBuffer, i * 32, 32);
            }

            return readBuffer;
        }
    }

Just a guess. Try declaring:

byte[] block = new byte[32];
and
byte[] readblock = new byte[32];

before the for() statement. I think it might be creating a new byte[32] for each loop. It doesn’t get garbage collected immediately so they are still hanging around in RAM. Just a guess anyway.

If a small project then around 350KB memory left which should have no problem.

Unless in your project it is free about 130-150KB only, then allocate one large 128KB, relate to fragment, or other thread not release memory yet, then it may failed.

Anyway, not recommended but as quick test below, it is run forever in our small project.

private static void TestConfiguration() {
            var config = new SecureStorageController(SecureStorage.Configuration);

            var cnt = 0;
            for (; ; )
            {
                GC.Collect();

                Debug.WriteLine("Round = " + cnt++);
                var data = new byte[config.TotalSize];
                for (var i = 0; i < 4096; i++) {
                    byte[] readBlock = new byte[32];
                    config.Read((uint)i, readBlock);
                    Array.Copy(readBlock, 0, data, i * 32, 32);
                }
            }
        }

GC.Collect(); helped, it works now during read but i still have some issues.