C# Bitmap/Graphics Out of Memory

前端 未结 1 937
温柔的废话
温柔的废话 2021-01-28 05:59

I\'m trying to take a snapshot of the whole screen for reading pixel values. Actually i\'m doing it without any problem. But after exactly 214 snapshots, i\'m getting out of mem

1条回答
  •  悲哀的现实
    2021-01-28 06:14

    You need to dispose disposable resources once you're done working with them. Bitmap class implements IDisposable - so it is disposable resource. Correct pattern is instead of

    Bitmap bmp = TakeSnapshot();
    var c = bmp.GetPixel(0,0);
    

    Something like

    Bitmap bmp = null;
    try
    {
      bmp = TakeSnapshot();
      var c = bmp.GetPixel(0,0);
      // any more work with bmp
    }
    finally
    {
      if (bmp != null)
      {
        bmp.Dipose();    
      }
    }
    

    Or in short form (which is preferable):

    using(Bitmap bmp = TakeSnapshot())
    {
      var c = bmp.GetPixel(0,0);
      // any more work with bmp
    }
    

    Reference: Using Objects That Implement IDisposable

    Edit

    You can easily emulate the issue:

    public class TestDispose : IDisposable
    {
        private IntPtr m_Chunk;
        private int m_Counter;
        private static int s_Counter;
    
        public TestDispose()
        {
            m_Counter = s_Counter++;
            // get 256 MB
            m_Chunk = Marshal.AllocHGlobal(1024 * 1024 * 256);
            Debug.WriteLine("TestDispose {0} constructor called.", m_Counter);
        }
    
        public void Dispose()
        {
            Debug.WriteLine("TestDispose {0} dispose called.", m_Counter);
            Marshal.FreeHGlobal(m_Chunk);
            m_Chunk = IntPtr.Zero;
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            for(var i = 0; i < 1000; i ++)
            {
                var foo = new TestDispose();
            }
            Console.WriteLine("Press any key to end...");
            Console.In.ReadLine();
        }
    }
    

    0 讨论(0)
提交回复
热议问题