On Windows 10, the System.Drawing.FontFamily.IsStyleAvailable method seems to leave the allocated space into memory even after the Dispose method has been called.
I wrote a simple console application to test it:
using System;
using System.Drawing;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static string getMemoryStatusString()
{
using (Process p = Process.GetCurrentProcess())
{
return "(p: " + p.PrivateMemorySize64 + ", v:" + p.VirtualMemorySize64 + ")";
}
}
static void Main(string[] args)
{
string s = getMemoryStatusString();
foreach(FontFamily fontFamily in FontFamily.Families)
{
Console.Write(fontFamily.Name + " " + getMemoryStatusString() + " -> ");
fontFamily.IsStyleAvailable(FontStyle.Regular);
fontFamily.Dispose();
Console.WriteLine(getMemoryStatusString());
}
string e = getMemoryStatusString();
Console.WriteLine(s + " -> " + e);
Console.ReadLine();
}
}
}
Any idea on why this is happening?
Thanks in advance!
Calling dispose doesn't release manage memory straight away. It has to wait till GC.Collect occurs. If you want to measure memory after Disposing it you should forcefully execute GC.Collect and wait for finalization.
Execute forceful garbage collection as below, before you take after disposing memory reading and see.
If there is a memory leak it would be in
gdiplus.dll
,FontFamily.IsStyleAvailable()
actually makes an extern call toGdipIsStyleAvailable()
.From ILSpy:
Which is in turn defined as:
I can't see any leak on my workstation (I use Windows 7 with .Net 4.5, however). There're some issues on the testing procedure
And I see that memory before equals to memory after: