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("HBP"))
{
sw.Write("1 ");
Console.Write("1 ");
}
else
{
sw.Write("1 ");
Console.Write("1 ");
}
using (Bitmap originalBMP = new Bitmap(filePath))
{
/***********************/
Bitmap imageBody;
ImageBody.ImageBody im = new ImageBody.ImageBody(originalBMP);
using (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");
}
}
sw.Flush();
}
sw.Dispose();
fs.Dispose();
}
}
}
views:
318answers:
8Shouldn't you be disposing imageBody as well?
I see it's opened:
Bitmap imageBody;
ImageBody.ImageBody im = new ImageBody.ImageBody(originalBMP);
imageBody = im.GetImageBody(-1);
But I don't see you disposing of it / setting it to null?
There are some Bitmap instances there that you aren't disposing. You should really try to get in the habit of using a using block rather than disposing manually, to stop these things slipping through the net.
Try to take the Bitmap originalBMP = new Bitmap(filePath); in a using()
using (Bitmap originalBMP = new Bitmap(filePath)) {
// your code....
sw.Flush();
}
All within the using() clause is definitly disposed after leaving the clause. You could also set you variables to null, after they are disposed.
You should use the using statement instead of Dispose() whereever possible. This way, you see in the declaration immediately that this instance you just create is freed.
Which is better?
Bitmap bmp = new Bitmap(filePath);
// .. pages of code goes here ..
bmp.Dispose(); // hopefully not forgotten
or
using (Bitmap bmp = new Bitmap(filePath))
{
// .. pages of code goes here ..
}
The using statement also ensures that all instances are freed even if you leave the current block/method prematurely with return,break or even an exception.
Note that you can put multiple assignments into the head of the using statement!
Maybe you are bitten by this bug: https://connect.microsoft.com/VisualStudio/feedback/details/521147/large-object-heap-fragmentation-causes-outofmemoryexception
In this case, adding
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
in the loop might help.
Or use sos.dll http://www.codeproject.com/KB/dotnet/Memory_Leak_Detection.aspx to see, where you leak memory.
You have a lot of calls to various classes, ImageBody,Rgb2Hsi,BlackPatchDetection etc
I assume these are you own code. Any of these could be holding on the resources.
I would suggest you grab a profiler and run some tests.
Most of them have trial versions giving you a couple of days with it.
http://stackoverflow.com/questions/49912/best-net-memory-and-performance-profiler
If you're getting the exception on this line:
using (Bitmap originalBMP = new Bitmap(filePath))
then that may mean you're trying to load an invalid or corrupted image file. For reasons known to no man, the OutOfMemoryException is what's thrown in this case. It actually has nothing to do with really being out of memory.
Try googling "bitmap.fromfile outofmemoryexception".
As with any Garbage collection issue, my approach would be to start commenting things out and seeing if I can still reproduce memory leaks. Things to try out:
- Instantiate only one instance of
Bitmapin the loop to see if the memory usage changes. - Dispose/not Dispose that one instance of Bitmap to see if disposing makes any difference.
From what I can see in the latest version of the code, whiteCoatBitmap, yellowCoatBitmap and blackPatchBitmap are not being disposed. Surround those with a using block as well.