FileSystem.writeTextToFile memory leak leads to System.OutOfMemoryException

Hi

Details:
Uno 1.8.1 (build 6782) Windows 10 x64 808e37c
Fuse 1.8.2 (build 15696)

In my project, data is persisted by serializing to JSON and writing data to file through FileSystem.writeTextToFile(). After some time, it will crash because of an OutOfMemoryException. Looking at the Android profiler, you’ll notice that the native memory keeps growing when using FileSystem.writeTextToFile().

This can easily be reproduced by running the below code. On my desktop PC in the Previewer it will run out of memory after around 1500 writes, on a Galaxy Tab A (2016) it runs out of memory after around 900 writes.

var someData = ""
var readWriteVars = {
	on: Observable(false),
	testCount: Observable(0)
}

function fnTestWrite() {
  readWriteVars.on.value = !readWriteVars.on.value

  // Generate long string (around 1mb) to make the leak more visible
  someData = "ABCDEFGHIJKLMNOPQRSTUVWXYZ \n"
  var iterations = 15
  for (var i = 0; i < iterations; i++) {
    someData = someData + someData
  }

  // Do 10 times in parallel for 10 files
  for(var i = 0; i < 10; i++) {
    fnDoTestWrite(FileSystem.dataDirectory + "/test_" + i)
  }

}

function fnDoTestWrite(path) {
  if(!readWriteVars.on.value) return
  
  readWriteVars.testCount.value = readWriteVars.testCount.value + 1

  // Save to path
  FileSystem.writeTextToFile(path, someData)
    .then(() => {
      fnDoTestWrite(path)
    })
}

fnTestWrite()

Thanks for looking into this.

FYI:
The same seems to be true for FileSystem.writeBufferToFile().

Replace the text with an Arraybuffer

// Arraybuffer data
someDataBuffer = new ArrayBuffer(1024 * 1024);

and write that to file

FileSystem.writeBufferToFile(path, someDataBuffer)