[Mono-dev] Memory Leak (Bitmap)Image.FromStream(memory_stream)

rgclickit rglassmeyer at clickitinc.com
Fri Mar 24 20:23:55 UTC 2017


Here is the code directly from the BitmapTest program:

            while (_runTest)
            {
                try
                {
                    // convert the byte[] array to a Bitmap using
Image.FromStream
                    // This will demonstrate a memory leak in Mono 4.6 and
4.8.
                    using (System.IO.MemoryStream ms = new
System.IO.MemoryStream(_sourceArray))
                    {
                        if (firstRun)
                        {
                            PaintStatus("First memory stream created OK
size: " + ms.Capacity.ToString());
                        }
                        using (Bitmap srcBitmap =
(Bitmap)Image.FromStream(ms))
                        {
                            if (firstRun)
                            {
                                PaintStatus("First Bitmap created OK size: "
+ srcBitmap.Size.ToString());
                                PaintStatus("Going to continue to run until
Stopped");
                            }
                            firstRun = false;
                        }
                    }
                    if (_forceGC)
                    {
                        GC.WaitForPendingFinalizers();
                        GC.Collect();
                    }
                }
                catch (Exception ex)
                {
                    PaintStatus("Error " + ex.Message);
                }
            }

The PaintStatus call:
        private void PaintStatus(string StatusMessage)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new status_info(PaintStatus), new object[] {
StatusMessage });
                    return;
                }
            }
            catch (Exception) { return; }

            try
            {
                string df = DateTime.Now.ToString("dd-MM hh:mm:ss");

                if (lstActivity.Items.Count > 2000)
                {
                    lstActivity.Items.RemoveAt(lstActivity.Items.Count - 1);
                }

                lstActivity.Items.Insert(0, df + " " + StatusMessage);
            }
            catch (Exception) { return; }
        }

When I started running:

<http://mono.1490590.n4.nabble.com/file/n4670336/starting.png> 

After 3 minutes of runtime from System Monitor
<http://mono.1490590.n4.nabble.com/file/n4670336/memory2.png> 


After 6 minutes of runtime from System Monitor
<http://mono.1490590.n4.nabble.com/file/n4670336/memory3.png> 

After 10 minutes of runtime from System Monitor
<http://mono.1490590.n4.nabble.com/file/n4670336/memory4.png> 

The memory keeps increasing and never drops. If I remove the memory stream
and use:

 using (Bitmap srcBitmap = (Bitmap)Image.FromFile(_file_name)) 

the memory usage doesn't change.

Again, thanks for any help you can provide.






--
View this message in context: http://mono.1490590.n4.nabble.com/Memory-Leak-Bitmap-Image-FromStream-memory-stream-tp4670327p4670336.html
Sent from the Mono - Dev mailing list archive at Nabble.com.


More information about the Mono-devel-list mailing list