FileSystem.Mount causes Memory Leak

Hello there,

I started to monitor the memory usage of my application and saw an increased usage over time.

I narrowed it down to mounting the SD-Card.

FileSystem.Mount() increases used memory by around 6kB that get’s collected after Unmount().
Sometimes FileSystem.Mount() increase used memory by an additional 80 / 288 bytes, while the 288 get collect by GC after a while, the 80 bytes stay for ever.

SD-Card is 32GB

Code I use:

    private static object m_lock = new object();
    private static StorageController m_controller;
    public static string DeviceName { get; private set; }
    public static bool Mounted { get; private set; }
    private static IDriveProvider m_drive;
    private static ArrayList m_user;
    private static int m_openCount;

    //--------------------------------------------------------------------------
    public static ref IDriveProvider OpenSD(string user)
    {
      lock (m_lock)
        lock (m_user)
        {
          Debug.WriteLine($"Inside SD-Card : {GC.GetTotalMemory(true)}");
          if (m_openCount == 0)
          {
            try
            {
              m_drive = FileSystem.Mount(m_controller.Hdc);
              DeviceName = m_drive.Name;
              Mounted = true;
              Debug.WriteLine($"After mounting : {GC.GetTotalMemory(true)}");
            }
            catch (InvalidOperationException e)
            {
              Debug.WriteLine("EXCEPTION SHALL NOT BE THROWN");
              if (e.Message != "Already mounted")
                throw e;
            }
          }
          if (!m_user.Contains(user))
          {
            m_user.Add(user);
            m_openCount++;
          }
          return ref m_drive;
        }
    }

Use my code below for testing memory:

var media = StorageController.FromName(FSSD_API_CONTROLLER);

            var cnt = 0;


            while (true)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                var drive = GHIElectronics.TinyCLR.IO.FileSystem.Mount(media.Hdc);

                DriveInfo driveInfo = new DriveInfo(drive.Name);


                //Debug.WriteLine("Free:  " + driveInfo.TotalFreeSpace);
                //Debug.WriteLine("TotalSize: " + driveInfo.TotalSize);
                //Debug.WriteLine("VolumeLabel:" + driveInfo.VolumeLabel);
                //Debug.WriteLine("RootDirectory: " + driveInfo.RootDirectory);
                //Debug.WriteLine("DriveFormat: " + driveInfo.DriveFormat);

                Thread.Sleep(1000);


                GHIElectronics.TinyCLR.IO.FileSystem.Unmount(media.Hdc);


 

                Debug.WriteLine("Mount counter = " + ++cnt + ", Memory Free = " + GC.GetTotalMemory(true));

               

            }

here is output and no leak found.

Isn’t your memory usage increasing every iteration?
I think the usage should be constant/ some should get collected after a while,
but not constantly increasing.

Mine runs out in about 2 days with that.

after a while, they should be constant.

Note: I changed the text “memory used”, not “memory free”.

If debugger is running, it will change memory used. The better way to test is add this code below, and let the board runs. if the led is on means memory has no change from last read, and of mean has changed.

var current_read = GC.GetTotalMemory(true);

                if (current_read != last_read)
                {
                    last_read = current_read;
                    led1.Write(GpioPinValue.Low);
                }
                else
                {
                    led1.Write(GpioPinValue.High);
                }

Thanks, you were right.
The increase of memory only occurs in debug mode.
After uploading a release version of the program the memory usage didn’t increase anymore.
Even if it’s annoying that I cannot run the program forever in Debug mode and watch what it is doing, I’m relieved that that doesn’t occur when releasing the application.

an alternative to help remove the annoyance factor is to use serial output to output relevant values so you can use a terminal program to watch…

I use a desktop application that communicates via TCP with the board to see the live log / transfer log files to my pc. So seeing the log is not an issue.
But thanks for the suggestion.