Memory leak when using Image.Save(Stream, ImageFormat)

I have an app that I need to compress RGB bitmap data into to byte array with JPEG format. So I use the Image.Save(Stream stream, ImageFormat foramt) to save into a MemoryStream and then convert to byte[]

using(MemoryStream ms = new MemoryStream())
using(Bitmap bmp = new Bitmap(640, 480, PixelFormat.Format24bppRgb)) {
    bmp.Save(ms, SaveFormat);
}

The code is fine with .NetFramework 4.0 on Windows 7, but when I run the same program with mono runtime on Ubuntu, the memory grows up and never get released.

I've tried upgrade the mono version but get same result.

Here is the full test code

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;

class Program {

    private static ImageFormat SaveFormat = ImageFormat.Jpeg;

    static void Main(string[] args) {

        bool isStopped = false;
        Thread t = new Thread(() => {
            while(!isStopped) {
                using(MemoryStream ms = new MemoryStream())
                using(Bitmap bmp = new Bitmap(640, 480, PixelFormat.Format24bppRgb)) {
                    bmp.Save(ms, SaveFormat);
                }
            }
        });
        t.Start();

        Console.ReadLine();
        isStopped = true;
        t.Join();
        Console.WriteLine("Stop");
    }
}

and the mono version.

user@Ubuntu:~$ mono --version

Mono JIT compiler version 5.4.1.6 (tarball Wed Nov  8 20:37:08 UTC 2017)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-
project.com
    TLS:           __thread
    SIGSEGV:       altstack
    Notifications: epoll
    Architecture:  amd64
    Disabled:      none
    Misc:          softdebug 
    LLVM:          supported, not enabled.
    GC:            sgen (concurrent by default)

I want to know is there any alternate solution to convert bitmap into jpeg format byte array? Or how to avoid memory leak on mono runtime

edit: I've tried with or without the GC, and still get memory leak

链接地址: http://www.djcxy.com/p/84154.html

上一篇: 如何确定UICollectionView flowLayout中单元格之间的间距

下一篇: 内存泄漏时使用Image.Save(流,ImageFormat)