Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everybody,

Since some days I have "out of memory" exception on my programs which had been working for years. (It might come from some updates...)

The weird part is they still works well on Windows XP 32 bits with much less memory. Both computers used for those programs are Windows 7 64 bits on dual core (2 GO RAM) and quad core (6 GO RAM). Thus I really doubt I miss real memory...

Each time I tried to work on few Images I have "out of memory" exception when it reaches 50% CPU or 25% CPU following the server. It's why I think it's a problem with multi core CPU...

Thanks in advance for any help,

Guillaume.
Posted

1 solution

The thing that initially jumps off the page at me is that you are working with images. If you do not do this correctly, you will leave the image open in memory (or even multiple copies if you do your variables wrong) and this will cause memory issues. I built an app that had a similar issue with memory. I traced it down to one place where I improperly opened an image in my code. That one error would make my application choke. To trace down these memory issues, I would recommend that you look at how to open images without leaking memory and then ensure that every place you open those images conforms to these best practices. If you still can't find something, try a memory profiler. Here is a great one that has a free demo:

http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/[^]

As to why this works on 32-bit when it doesn't on 64-bit, I would say that it could be because of how the application is compiled (32-bit vs. AnyCPU). I don't think this is the real problem. I believe your application is leaking memory either way and the 64-bit computers are just the ones showing it. If not, that memory profiler would point out the issue with the 64-bit computers.

Finally, you are mixing two different pieces together here. Memory and CPU are not the same thing. Having several cores will not help/harm a memory issue. Memory (RAM) is for volatile storage of items (data that will be destroyed soon like an image that you are working with until you save it to disk) while the CPU (cores) is for calculations (identifying how to transform the image). A faster CPU will reduce memory issues in that it will allow certain things to be dumped out of RAM faster because they are processed faster. However, in general, these are two separate topics. In your case, the issue is memory (RAM), not processor.
 
Share this answer
 
Comments
geam666 13-Jun-12 7:37am    
Thanks for your reply Tim, the weird part it those applications had worked well on Windows 7 64 bits for nearly a year, I thought it come from memory because each time it crashed at about 25% or 50% following the server...

It's true I try to reduce images with the following code (declaration part removed to be readable in English) where I had already added the Dispose calls months ago :

public static void ReduceImagesFiles()
{
if (!Directory.Exists(ReducedImagesDirectory)) Directory.CreateDirectory(ReducedImagesDirectory);

foreach (String SourceImagePath in Directory.GetFiles(sourceDirectory))
{
sb = new StringBuilder(Path.GetFileNameWithoutExtension(SourceImagePath));
sb.Append(Path.GetExtension(SourceImagePath).ToLower());
imageName = sb.ToString();

if (imageName.ToLower().EndsWith(".jpg") || imageName.ToLower().EndsWith(".png") || imageName.ToLower().EndsWith(".gif"))
{
NewHeight = MAX_HEIGHT;
NewWidth = MAX_WIDTH;

DestinationFilePath = Path.Combine(ReducedImagesDirectory, imageName);

System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(SourceImagePath);

// Prevent using images internal thumbnail
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

if (FullsizeImage.Width <= MAX_WIDTH)
{
NewWidth = FullsizeImage.Width;
}

NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
if (NewHeight > MAX_HEIGHT)
{
NewWidth = FullsizeImage.Width * MAX_HEIGHT / FullsizeImage.Height;
NewHeight = MAX_HEIGHT;
}

//throw Exception on this line
System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

// Clear handle to original file so that we can overwrite it if necessary
FullsizeImage.Dispose();

// Save resized picture
NewImage.Save(DestinationFilePath);

NewImage.Dispose();
}
}
}

I had received some hints from the following link too :
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/92ba3d50-ba6b-4b4b-99ee-19e7fc8b50da
geam666 13-Jun-12 8:34am    
It was not the issue but it's still some interesting tips...

After doing some traces, I had seen the program always crashed on the same image.

I think it might be by security VS 2008 uses only one core though I wonder if we can ask for more...

So on this image I have some odd parameters, it would be an Adobe CS5 Macintosh image with LZW compression, do you think it can cause trouble to our code and is there any way to deal with it.

I guess the error message pointed me out on the wrong direction...

Thanks in advance,

Guillaume.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900