Garbage Collector not doing its job. Memory Consumption = 1.5GB & OutOFMemory Exception.

Posted by imageWorker on Stack Overflow See other posts from Stack Overflow or by imageWorker
Published on 2010-04-01T10:54:47Z Indexed on 2010/04/01 11:03 UTC
Read the original article Hit count: 348

Filed under:
|

I'm working with images (each of size >= 5MB). The following code extract some information from each image that is present in the given directory. I'm getting out of memory exception. The size of the process is around (1.5GB). I don't know why garbage collector is not freeing memory.

I even tried adding GC.Collect() as last line of foreach loop. Still I'm getting 'OutOFMemory'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace TrainSVM
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fs = new FileStream("dg.train",FileMode.OpenOrCreate,FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            String[] filePathArr = Directory.GetFiles("E:\\images\\");

            foreach (string filePath in filePathArr)
            {
                if (filePath.Contains("lmn"))
                {
                    sw.Write("1 ");
                    Console.Write("1 ");
                }
                else
                {
                    sw.Write("1 ");
                    Console.Write("1 ");
                }

                Bitmap originalBMP = new Bitmap(filePath);


                /***********************/
                Bitmap imageBody;
                ImageBody.ImageBody im = new ImageBody.ImageBody(originalBMP);
                imageBody = im.GetImageBody(-1);

                /* white coat */
                Bitmap whiteCoatBitmap = Rgb2Hsi.Rgb2Hsi.GetHuePlane(imageBody);
                float WhiteCoatPixelPercentage = Rgb2Hsi.Rgb2Hsi.GetWhiteCoatPixelPercentage(whiteCoatBitmap);
                //Console.Write("whiteDone\t");
                sw.Write("1:" + WhiteCoatPixelPercentage + " ");
                Console.Write("1:" + WhiteCoatPixelPercentage + " ");

                /******************/
                Quaternion.Quaternion qtr = new Quaternion.Quaternion(-15);
                Bitmap yellowCoatBMP = qtr.processImage(imageBody);
                //yellowCoatBMP.Save("yellowCoat.bmp");
                float yellowCoatPixelPercentage = qtr.GetYellowCoatPixelPercentage(yellowCoatBMP);
                //Console.Write("yellowCoatDone\t");
                sw.Write("2:" + yellowCoatPixelPercentage + " ");
                Console.Write("2:" + yellowCoatPixelPercentage + " ");

                /**********************/
                Bitmap balckPatchBitmap = BlackPatchDetection.BlackPatchDetector.MarkBlackPatches(imageBody);
                float BlackPatchPixelPercentage = BlackPatchDetection.BlackPatchDetector.BlackPatchPercentage;
                //Console.Write("balckPatchDone\n");
                sw.Write("3:" + BlackPatchPixelPercentage + "\n");
                Console.Write("3:" + BlackPatchPixelPercentage + "\n");

                balckPatchBitmap.Dispose();
                yellowCoatBMP.Dispose();
                whiteCoatBitmap.Dispose();
                originalBMP.Dispose();
                sw.Flush();

            }


            sw.Dispose();
            fs.Dispose();

        }

    }
}

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#