Skip to content

Commit

Permalink
Add a code to reproduce the problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Sulimov committed Jun 3, 2021
0 parents commit 3c70f9a
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin
obj
89 changes: 89 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading;

namespace TestImages
{
static class Program
{
static byte[] _jpegBytes;
static EventWaitHandle _evt = new AutoResetEvent(false);
static bool _running = true;

static void Main(string[] args)
{
int imagesOnCollage = int.Parse(args.FirstOrDefault() ?? "10");
int aCollageDrawingPeriodMs = int.Parse(args.Skip(1).FirstOrDefault() ?? "100");

//ThreadPool.SetMinThreads(100, 100);

Console.CancelKeyPress += (s, e) =>
{
_running = false;
e.Cancel = true;
};

_jpegBytes = File.ReadAllBytes("image.jpg");

while (_running)
{
var sw = Stopwatch.StartNew();
DrawCollageImages(imagesOnCollage);
int spent = (int)sw.Elapsed.TotalMilliseconds;
int left = aCollageDrawingPeriodMs - spent;
if (left < 0) left = 0;
Thread.Sleep(left);
}

_evt.Dispose();
}

static void DrawCollageImages(int count)
{
ThreadPool.QueueUserWorkItem(_ =>
{
Console.WriteLine("thread id {0,4}, threads count {1,3}",
Thread.CurrentThread.ManagedThreadId, ThreadPool.ThreadCount);
using (var collage = new Bitmap(1000, 1000))
{
DrawCollageImagesImpl(collage, count);
}
_evt.Set();
Thread.Sleep(20000); // prevent this thread id reuse
}
);
_evt.WaitOne();
}

static void DrawCollageImagesImpl(Image collage, int count)
{
using (var g = Graphics.FromImage(collage))
{
g.DrawCollageImages(count);
}
}

static Rectangle _rect = new Rectangle(0, 0, 500, 500);

static void DrawCollageImages(this Graphics g, int count)
{
for (int i=0; i<count; i++)
{
g.DrawCollageImage(_rect);
}
}

static void DrawCollageImage(this Graphics g, Rectangle r)
{
using var ms = new MemoryStream(_jpegBytes);
using var img = Image.FromStream(ms);
g.DrawImage(img, r.X, r.Y, r.Width, r.Height);
}
}
}
18 changes: 18 additions & 0 deletions SystemDrawingMultithreadingMemoryLeak.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
</ItemGroup>

<ItemGroup>
<None Update="*.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file added image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3c70f9a

Please sign in to comment.