none
FontFamily Memory Leak on Windows 10 RRS feed

  • Question

  • Hi,

    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!

    Thursday, October 29, 2015 2:24 PM

All replies

  • Hi Mirko,

    Thank you for your post. VirtualMemorySize64 and PrivateMemorySize64. I am afraid none of these values are a reliable indicator of how much memory an executable is actually using, and none of them are really appropriate for debugging a memory leak. For more information, link below is for your reference.
    http://stackoverflow.com/questions/1984186/what-is-private-bytes-virtual-bytes-working-set

    You could use some profiler tools such as the CLR Profiler or ANTS Memory Profiler to detect memory leaks.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 30, 2015 5:33 AM
    Moderator
  • Hi Li,

    Thanks for your answer!

    I wrote this script just to highlight the problem in the simplest way. Using ANTS Memory Profiler gave me a complete view of the problem but essentially the same result.

    Testing this simple console application on a Windows 10 instance with nearly 6000 fonts installed resulted in an increase to 1.8GB of Virtual Memory.

    Regards,

    Mirko

    Friday, October 30, 2015 8:47 AM
  • Hi Mirko,

    >>increase to 1.8GB of Virtual Memory.

    I tested your code in Win10. There are 210 fonts on my side. So I execute your below code 30 times.

    foreach(FontFamily fontFamily in FontFamily.Families)
    {
         Console.Write(fontFamily.Name + " " + getMemoryStatusString() + " -> ");
         fontFamily.IsStyleAvailable(FontStyle.Regular);
         fontFamily.Dispose();
         Console.WriteLine(getMemoryStatusString());
    }

    But I didn't found big growth of Virtual Memory increase. Below is my data.  Did you find which font will cause big growth of Virtual Memory.

    (p: 15536128, v:194850816) -> (p: 22253568, v:219406336)
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 2, 2015 7:43 AM
    Moderator
  • Hi Li,

    >>I tested your code in Win10. There are 210 fonts on my side. So I execute your below code 30 times.

    The leak is related to each FontFamily, if you call IsStyleAvailable more times on the same FontFamily, the memory increases on the first call only.

    Thanks,

    Mirko

    Monday, November 2, 2015 10:35 AM
  • Hi Mirko,

    I will continue pay attention to this issue. In addition, I suggest you post your issue on below site for more help.
    https://connect.microsoft.com/visualStudio/

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 3, 2015 5:16 AM
    Moderator