when creating thumbnails for webp files unmanaged memory is not released
Problem reported by michael finarov - 8/24/2020 at 4:00 AM
Being Fixed
hi
we're creating thumbnails for 1000 webp files and the unmanaged memory raising to 2.6gb and is not release 
until we close the application
it's also happens on gif files but with much less memory per file

11 Replies

Reply to Thread
0
Cem Alacayir Replied
Employee Post
Are you sure you are disposing ImageTask object?

using (var imageTask = new ImageTask(@"C:\Pictures\Picture1.jpg"))
    imageTask.Thumbnail(160).Save(@"C:\Output\Thumbnail1.jpg");
0
michael finarov Replied
yes
once i do resize or just call for the info property
 using (var imageTask = new ImageTask(stream, null, false, _configuration))
                {
                    if (imageTask.Info != null)
                    {
                        imageFormat = imageTask.Info.Format;
                        var actualsize = GetThumbSize(imageTask.Info.Width, imageTask.Info.Height, size);
                        drawingImage = imageTask.Resize(actualsize.Width, actualsize.Height).ToBitmap();
                    }
                    else
                    {
                        drawingImage = imageTask.Resize(size, size).ToBitmap();
                    }
}
the memory is consumed even if i don't call the to bitmap just resize
0
Cem Alacayir Replied
Employee Post
Well you are calling ToBitmap() method which returns a System.Drawing.Bitmap and you are responsible for disposing that Bitmap copy when you are done with it. So in your code you also need to dispose drawingImage   variable.
0
michael finarov Replied
even i  do only if (imageTask.Info != null) without the rest of the code
the leak occur
i remove the tobitmap()
and removed the drawingimage
and the leak still occour
the leak happen only on webp files
0
michael finarov Replied
the leak is of unmanged memory
0
Cem Alacayir Replied
Employee Post
Can you confirm you are using latest Version 4.2.3 - August 6, 2020 ?

So you say, the leak occurs only when you access imageTask.Info property like this?

using (var imageTask = new ImageTask(stream, null, false, _configuration))
{
    if (imageTask.Info != null)
    {
    
    }
}
There is no known issue with imageTask.Info in recent versions. Make sure you check memory after imageTask is disposed, i.e. don't put breakpoint inside using clause, of course. 

If you can duplicate the leak issue with the latest version, please send me the sample WEBP file (by the way you said first it also happens with GIF file?)

0
michael finarov Replied
tested on latest version 4.2.3 and just called the info and got the same 2.6gb of unmanaged memory leak
i'll try and find which of the 1000 files cases the most leak and i'll post it
on gif files we got 500mb of unmanaged memory leak for 700 files
other formats works fine
0
Cem Alacayir Replied
Employee Post
Where does your stream come from as in

using (var imageTask = new ImageTask(stream, null, false, _configuration))
Are you disposing the stream too? Does the leak also occur when you use a file path?

0
michael finarov Replied
we can't use file path we don't have file on disk
i'll run test for files form disk to see if the issue still exist but it's not a senario we can use
 the flow is the same for all files and only webp and minor issue on gif files creates the memory leak
as mentioned above even if i don't create thumbnails just call the Info property leak occur for webp files
 tested on the same code using file path and file stream and using the stream got the leak
attaching792cbd3eec601a96b2ab215aadf55b86 file that causes ~30mb of leak
0
Cem Alacayir Replied
Employee Post
Before testing your file:

> using the stream got the leak 

I asked if you are sure you are disposing your own stream like this? 

using (stream)
using (var imageTask = new ImageTask(stream, null, false, _configuration))
{
    if (imageTask.Info != null)
    {
    
    }
}
Because you say leak does not happen with file path, so probably the leak comes from your own "not disposed" stream. ImageTask will not dispose your stream internally. In .NET Framework as best practice, the classes will not dispose/close your input stream. If you open a stream, you need to ensure closing it after used.

> as mentioned above even if i don't create thumbnails just call the Info property leak occur for webp files

FYI, accessing Info property also means reading the image file from stream.
0
michael finarov Replied
 string path = @"C:\test\792cbd3eec601a96b2ab215aadf55b86";
            //Leak
            using (FileStream fs = File.OpenRead(path))
            {
                using (var imageTask = new ImageTask(fs, null, false))
                {
                    if (imageTask.Info != null)
                    { }
                }
            }

            //No Leak
            using (var imageTask = new ImageTask(path, null, false))
            {
                if (imageTask.Info != null)
                { }
            }
again the leak is for webp file type

Reply to Thread