Page 1 of 1

memory leak issue of ResizeNearestNeighbor API

PostPosted: Tue Jan 08, 2019 5:50 am
by Bhumit
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.

Re: memory leak issue of ResizeNearestNeighbor API

PostPosted: Tue Jan 08, 2019 8:22 am
by andrew.kirillov
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.

Re: memory leak issue of ResizeNearestNeighbor API

PostPosted: Wed Jan 09, 2019 2:05 pm
by Bhumit
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,

Re: memory leak issue of ResizeNearestNeighbor API

PostPosted: Wed Jan 09, 2019 2:25 pm
by andrew.kirillov
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.

Re: memory leak issue of ResizeNearestNeighbor API

PostPosted: Thu Jan 10, 2019 6:53 am
by Bhumit
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 ?

Re: memory leak issue of ResizeNearestNeighbor API

PostPosted: Thu Jan 10, 2019 8:14 am
by andrew.kirillov
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.