Possible memory leak in using System.Security.Cryptography.HashAlgorithm;?

I have a memory leak in my application and investigating it has led me to believe there is a memory leak in netmf’s HashAlgorithm.


using System;
using Microsoft.SPOT;
using System.Security.Cryptography;
using System.Threading;

namespace MemoryLeakTest
    public class Program
        public static void Main()
            var text = "This is a test string to be hashed, to investigate if there's a memory leak";

            uint availableMemory;
            byte[] hash;
            while (true)
                using (var hashAlg = new HashAlgorithm(HashAlgorithmType.MD5))
                    hash = hashAlg.ComputeHash(System.Text.Encoding.UTF8.GetBytes(text));
                availableMemory = Debug.GC(true);


Running that lets me watch the available memory decrease steadily for every loop.

Setting Debug.EnableGCMessages(true); and checking the GC output one can see that its
thats growing.

Would appreciate if someone could verify this.

The ironic thing is that I use the System.Security.Cryptography.HashAlgorithm to sign my http-messages since there is a memory leak when using SSL. It strikes me now though… could the leaks be connected?

@ RandomNumber - It’s certainly possible the leaks are connected. Can you try your code in the NETMF emulator?

@ John -

Running it with emulator, the leak is no longer visible. Every lopp prints same amount of available memory.

(My board is G120)

Try to call Clear() method of the HashAlgorithm.

@ RandomNumber - We are able to reproduce the issue, though we haven’t looked into the cause yet. I would not be surprised if it has the same root cause as the SSL leak. It worked in the emulator because the emulator does not use the same encryption stack as the actual device does.