Bug 54149 - System.Drawing.Image.FromStream Memory Leak - With Repo Code
Summary: System.Drawing.Image.FromStream Memory Leak - With Repo Code
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Drawing ()
Version: 4.8.0 (C9)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2017-03-28 18:54 UTC by Ray
Modified: 2018-05-02 21:18 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 54149 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Ray 2017-03-28 18:54:12 UTC
The System.Drawing.Image.FromStream has a small memory leak. The code below can demonstrate this problem. System Monitor will show the memory usage increase the longer this program is run.

Create a Console project (FromStreamMemoryBug)
Add a reference to System.Drawing. 
Copy/paste the code below into Program.cs. 

The code requires some local image (BMP, PNG, etc.) files.

using System;
using System.Drawing;
using System.IO;
namespace FromStreamMemoryBug
    class Program
        static void RunTest(string file_name)
			using (var stream = File.OpenRead (file_name)) {
				using (var p = Image.FromStream (stream)) {
					// Console.WriteLine("Image Size " + p.Size.ToString());

        static void Main(string[] args)
			// BUG tested in Mono 4.6, and 4.8
			// current Mono version information:
			//			Mono JIT compiler version 4.8.0 (Stable Wed Mar 15 16:18:35 UTC 2017)
			//			Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
			//			TLS:           __thread
			//			SIGSEGV:       altstack
			//			Notifications: epoll
			//			Architecture:  amd64
			//			Disabled:      none
			//			Misc:          softdebug 
			//			LLVM:          supported, not enabled.
			//			GC:            sgen

			// OS Information
			//			Distributor ID:	Ubuntu
			//			Description:	Ubuntu 16.04.2 LTS
			//			Release:	16.04
			//			Codename:	xenial

			// Name the main thread to make it easy to find in System Monitor
			System.Threading.Thread.CurrentThread.Name = "FSMemTest";

			// Pick your own image file(s)
			string file1 = Path.Combine (AppDomain.CurrentDomain.BaseDirectory, "image0.bmp");
			string file2 = Path.Combine (AppDomain.CurrentDomain.BaseDirectory, "testimage.png");

			int maxCount = 5000;
			Console.WriteLine ("Use System Monitor to look at the memory usage of FSMemTest");
			Console.WriteLine ("... Press any key to Start ...");
			Console.ReadKey ();
			for (int i = 0; i < maxCount; i++) {
				if (i % 100 == 0) {
					Console.WriteLine ("RunTest " + i.ToString () + " of " + maxCount.ToString ());
				RunTest (file1);
				RunTest (file2);
				// Try with and without calling garbage collection
				 GC.Collect ();
			Console.WriteLine ("Done with the test.");
			Console.WriteLine ("... Press any key to exit ...");
			Console.ReadKey ();
Comment 1 Ray 2017-04-03 17:48:50 UTC
When running this program for about 15 minutes, going through the loop 50,000 times, System Monitor showed the memory leak was 150 MiB.

This represents work for about 1 hour of images (7 images per second for 2 cameras). I believe this is represents a large memory leak, not a small leak as I originally stated. And it makes Image.FromStream unusable when working with the quantity of images that can be generated from a video sources.
Comment 2 Bongho Lee 2017-05-11 05:50:47 UTC
I got also same memory leak from Image.FromStream method.
My mono version is 4.8.1.
Comment 3 joshua.sells 2018-05-02 20:46:43 UTC
I also get this using ImageSource.FromStream.

Xamarin.Forms v

OS: Windows 10 Version 1709 (UWP)
Comment 4 joshua.sells 2018-05-02 21:12:18 UTC
Update: when running with the Profiler (VS) attached... no leak!
Comment 5 joshua.sells 2018-05-02 21:18:43 UTC
Ok, so the leak only appears to occur when debugger is attached. Profiler != Debugger, and running the app without debugging at all seems fine.

Very strange!