AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

memory leak issue of ResizeNearestNeighbor API

The forum is to discuss topics related to different areas of image processing and computer vision.

memory leak issue of ResizeNearestNeighbor API

Postby Bhumit » Tue Jan 08, 2019 5:50 am

Hello All,

We need your help to resolve below issue.

Our process is to get the text from image document and we are doing preImageProcessing by using ResizeNearestNeighbor Aforge API.

To do preImageProcessing, we are scaling height and width of image to 3 times (Width * 3) (Height * 3) using ResizeNearestNeighbor.

But if image size gets big after scalling 3 times(ex, Widht = 7500 and height= 10500) then we facing memory leak issue.

We are able to do resize the image whose width and height are small. (for ex, width*3 = 6000 and Height*3 = 8000)

Please see the below code snippet,

BaseResizeFilter resizeFilter = new ResizeNearestNeighbor((int)(processedBitmap.Width * 3), (int)(processedBitmap.Height * 3));
processedBitmap = resizeFilter.Apply(processedBitmap);

Aforge Version : 2.2.4

Thanks in advance.
Bhumit
 
Posts: 3
Joined: Tue Jan 08, 2019 5:36 am

Re: memory leak issue of ResizeNearestNeighbor API

Postby andrew.kirillov » Tue Jan 08, 2019 8:22 am

Hello,

The code in ResizeNearestNeighbor does all the same no matter of image size. So if there is a memory leak, it will show up anyway.

How do you find that you have a memory leak? Are you disposing resources you allocate (temporary images)? What may happen is that you don't dispose temporary images and so memory usage grows faster than garbage collector cleans up everything.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3447
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: memory leak issue of ResizeNearestNeighbor API

Postby Bhumit » Wed Jan 09, 2019 2:05 pm

Here is the code with we are trying to process/convert 8bpp image to 1bpp and we are getting "out of memory exception" in BMPIn.GetHbitmap() function. Also attached the image.

Please look below code for the same and help me to find out the cause for the memory leak.

Code: Select all
private void button1_Click(object sender, EventArgs e)
        {
            for(int i = 0; i<= 100; i++)
            {
                this.Text = "Counter :" + i;
                Binarize();
            }
        }

        public void Binarize()
        {
                string imagePath = "D:\\temp\\WWC 2017.JPG";
                Bitmap processedBitmap = new Bitmap(imagePath);
                processedBitmap = applyFilter(imagePath);
                BaseResizeFilter resizeFilter = new ResizeNearestNeighbor((int)(processedBitmap.Width * 3), (int)(processedBitmap.Height * 3));
                processedBitmap = resizeFilter.Apply(processedBitmap);
                autoThreshold(processedBitmap);
                string guid = Guid.NewGuid().ToString();
                processedBitmap.Save($"D:\\temp\\{guid}.bmp");
                processedBitmap.Dispose();

                Bitmap BMPIn = new Bitmap($"D:\\temp\\{guid}.bmp");
                IntPtr hbmIn = BMPIn.GetHbitmap();
                // logic for 8bpp to 1bpp
        }
        private void autoThreshold(Bitmap processedBitmap)
        {
            OtsuThreshold otsu = new OtsuThreshold();
            otsu.ApplyInPlace(processedBitmap);
        }
        private Bitmap applyFilter(string imagePath)
        {
            Bitmap processedBitmap;
            using (Bitmap orgBitmap = Image.FromFile(imagePath) as Bitmap)
            {
                try
                {
                    BrightnessCorrection brightnessCorrection = new BrightnessCorrection(25);
                    brightnessCorrection.ApplyInPlace(orgBitmap);
                    ContrastCorrection contrastCorrection = new ContrastCorrection(25);
                    contrastCorrection.ApplyInPlace(orgBitmap);
                    Sharpen sharpen = new Sharpen();
                    sharpen.ApplyInPlace(orgBitmap);
                    processedBitmap = Grayscale.CommonAlgorithms.RMY.Apply(orgBitmap);
                }
                catch
                {
                    processedBitmap = orgBitmap;
                }
            }
            return processedBitmap;
        }


Thanks in advance,
Bhumit
 
Posts: 3
Joined: Tue Jan 08, 2019 5:36 am

Re: memory leak issue of ResizeNearestNeighbor API

Postby andrew.kirillov » Wed Jan 09, 2019 2:25 pm

Well, from the code it looks like you don't really free resources.

>> Bitmap processedBitmap = new Bitmap(imagePath);
2nd line in Binarize() - leak. You don't do anything with this image - the variable gets new value on the next line

>> Bitmap processedBitmap = new Bitmap(imagePath);
3rd line in Binarize() - leak. The processedBitmap will get new value later when resize filter is used.

>> Bitmap BMPIn = new Bitmap($"D:\\temp\\{guid}.bmp");
leak, cannot see you dispose it.

All temporary images must be disposed.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3447
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: memory leak issue of ResizeNearestNeighbor API

Postby Bhumit » Thu Jan 10, 2019 6:53 am

Hi @Andrew.Kirillov,

Thanks for the quick response.

for below 2nd and 3rd line, We have already written a dispose() method for processedBitmap at line no 9 in Binarize() method.
processedBitmap.Dispose();

also used using keypword in applyFilter() method.


>> Bitmap processedBitmap = new Bitmap(imagePath);
2nd line in Binarize() - leak. You don't do anything with this image - the variable gets new value on the next line

>> Bitmap processedBitmap = new Bitmap(imagePath);
3rd line in Binarize() - leak. The processedBitmap will get new value later when resize filter is used.

For below, We tried to dispose BMPIn bitmap, but still we are getting the out of memory exception after 2nd iteration.

>> Bitmap BMPIn = new Bitmap($"D:\\temp\\{guid}.bmp");
leak, cannot see you dispose it.

Please guide me, where should i release the memory ?
Bhumit
 
Posts: 3
Joined: Tue Jan 08, 2019 5:36 am

Re: memory leak issue of ResizeNearestNeighbor API

Postby andrew.kirillov » Thu Jan 10, 2019 8:14 am

Bhumit wrote:We have already written a dispose() method for processedBitmap at line no 9 in Binarize() method.

So what? It only disposes the last image. The processedBitmap gets set many times. You dispose only the last image. All others are leaking.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3447
Joined: Fri Jan 23, 2009 9:12 am
Location: UK




Return to Image Processing and Computer Vision