[Mono-dev] Memory Leak (Bitmap)Image.FromStream(memory_stream)

Robert Jordan robertj at gmx.net
Fri Mar 24 22:06:27 UTC 2017


On 24.03.2017 20:17, Robert Jordan wrote:
> On 23.03.2017 21:51, rgclickit wrote:
>> I believe I discovered a memory leak when creating Bitmaps from the
>> Image.FromStream call. The basic code to reproduce the problem is:
>>
>> using (System.IO.MemoryStream ms = new
>> System.IO.MemoryStream(_sourceArray))
>> {
>>      using (Bitmap srcBitmap = (Bitmap)Image.FromStream(ms)) {
>>      }
>> }
>>
>
> I cannot reproduce this. The odds are that you're not computing
> the memory usage correctly.
>
> See
> http://www.mono-project.com/docs/advanced/performance-tips/#understanding-memory-usage


I was testing with a too large bitmap and hence too few iterations/sec.
With a 1x1 bitmap the leak started to become evident, so it
probably depends on how often Image.FromStream is getting called.

The code behind Image.FromStream is pretty complex because the
underlying unmanaged library (libgdiplus) knows nothing about
managed streams.

Hard to say, where to leak is. It might be an unmanaged one...

You may want to file a bug, ideally with a test case which doesn't
depend on WinForms, like this one:

---
using System;
using System.Drawing;
using System.IO;

class Program
{
	static void Run ()
	{
		using (var stream = File.OpenRead ("test.bmp"))
			using (Image.FromStream (stream)) {
                 }
	}

         static void Main ()
         {
                 for (int i = 0; i < 100000; i++)
                         Run ();
         }
}
---

Robert




More information about the Mono-devel-list mailing list