Skip to content

Commit

Permalink
Merge pull request #1699 from cwensley/curtis/wpf-graphics-on-bitmap-…
Browse files Browse the repository at this point in the history
…is-slow

Wpf: Fix huge performance penalty using Graphics on many Bitmaps
  • Loading branch information
cwensley authored May 27, 2020
2 parents ca76382 + dc78bd2 commit 780e081
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Eto.Wpf/WpfExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,11 @@ public static void RenderWithCollect(this swm.Imaging.RenderTargetBitmap bitmap,
// fix memory leak with RenderTargetBitmap. See http://stackoverflow.com/questions/14786490/wpf-memory-leak-using-rendertargetbitmap
// Reproducible with the
// GC.Collect alone seems to fix the issue. Adding GC.WaitForPendingFinalizers may impact performance.
GC.Collect();

// Note: this may no longer be an issue with the latest version of .NET after some testing, however
// we keep it here to keep memory in check, but instead make it non-blocking so it doesn't cause a huge
// performance penalty creating many Bitmaps with a Graphics object.
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, false);
}


Expand Down
31 changes: 31 additions & 0 deletions test/Eto.Test.Wpf/UnitTests/BitmapTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Diagnostics;
using Eto.Drawing;
using Eto.Test.UnitTests;
using NUnit.Framework;

namespace Eto.Test.Wpf.UnitTests
{
[TestFixture]
public class BitmapTests : TestBase
{
[Test, Timeout(1000)]
public void CreatingManySmallBitmapsShouldBeFast()
{
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 100; i++)
{
var bmp = new Bitmap(20, 20, PixelFormat.Format32bppRgba);

using (var g = new Graphics(bmp))
{
g.Clear(Colors.Blue);
}
}
sw.Stop();
Console.WriteLine($"Total time: {sw.Elapsed}");
}

}
}

0 comments on commit 780e081

Please sign in to comment.