Righto brains trust - how do we play with large objects in the unmanaged heap?
I can happily read a 70kb file into a byte array from SD but want to then create a string from the byte[] and promptly run out of memory, so i assume we need to play in the unmanaged heap, but buggered if i know how to do it proper like.
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using GHIElectronics.TinyCLR.Devices.Storage;
using GHIElectronics.TinyCLR.IO;
using GHIElectronics.TinyCLR.Native;
namespace USBTest
{
class Program
{
private static byte[] _data;
static void Main()
{
UnmanagedBuffer uBuffer = new UnmanagedBuffer(100000);
byte[] uTable = uBuffer.Bytes;
var sd = StorageController.FromName(@"GHIElectronics.TinyCLR.NativeApis.STM32H7.SdCardStorageController\0");
string file = $@"{drive.Name}BP2.xml";
if (File.Exists(file))
{
using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read))
{
uTable = new byte[(int)fs.Length];
fs.Read(uTable, 0, (int)uTable.Length);
}
}
FileSystem.Flush(sd.Hdc);
string str = new string(Encoding.UTF8.GetChars(uTable)); // death by lack of ram
uTable = null;
uBuffer.Dispose();
Thread.Sleep(Timeout.Infinite);
}
}
}