0

I am working on image processing project,In my application i am processing one by one images from folder(i am processing images in a while loop), folder contains more than 1000 images.i am using the garbage collection(GC.Collect()) for every 4 image count. after processing of 1000 images i a getting OutOfMemoryException(the exception will through when bitmap image is assigning to a picturebox),how to solve this problem?

Praveen Kumar
  • 1,576
  • 8
  • 31
  • 47
  • What function generates the error? – Gabe Feb 09 '12 at 06:37
  • the exception will through when bitmap image is assigning to a picturebox – Praveen Kumar Feb 09 '12 at 06:46
  • 1
    You should never ever use GC.Collect().. , especially not in loops. This can make really troubles, upgrades all your object generations, and simply makes the GC useless at all.. It even may cause the problem itself... See the other solutions metioned below and remove the collect method – Boas Enkler Feb 09 '12 at 07:03
  • +1. This is a valid question - not many developers know about the inner workings of .NET memory management and there is an issue here with alrge bitmaps. This is not necessarily a stupid question - one showing some ignorance, but when 95% of the people don't know the asnwer, this is not ignorance but a valid point. – TomTom Feb 09 '12 at 07:05
  • so then when can we use GC.Collect() – Praveen Kumar Feb 09 '12 at 07:14

3 Answers3

1

Neither noew. It is toally ok to get those exceptions without doing soething bad. Large Heap Fragemntation is still an unsolved problem.

Your best chance is to open a separate process that works on images until it runs out (then restart it) under the control of your main application, and / or move to 64 bit (larger space makes fragmentation issues less likely).

the exception will through when bitmap image is assigning to a picturebox

This needs a lot of memory in one chunk. Tell us about those images? How large are they? Why picturebox (batch processing normally does not need to see the image).Anyhow, this is adefined problem and if thee images are large it is to be expected.

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • one image size is 1MB(bitmap image) – Praveen Kumar Feb 09 '12 at 07:01
  • You mean 1mb in memory, or on disc? What is the memory size? 1mb in memory is not a problem, a 1mb image on disc can be hundreds of mb in memory and then you run into the fragemtation issue. Check http://connect.microsoft.com/VisualStudio/feedback/details/521147/large-object-heap-fragmentation-causes-outofmemoryexception for an example of the issue. – TomTom Feb 09 '12 at 07:04
0

That depends on how you are using the images. Chances are that you are not disposing the relevant instance. Try wrapping your instances in a using statement:

using (var image = ...new instance created...)
{
    // ...do stuff...
}
Eben Roux
  • 12,983
  • 2
  • 27
  • 48
0

Look for memory leaks. You can use WinDbg with gcroot command. See the link below.

http://blogs.msdn.com/b/delay/archive/2009/03/11/where-s-your-leak-at-using-windbg-sos-and-gcroot-to-diagnose-a-net-memory-leak.aspx

You need to investigate suspects. You can use SOS debugging extension. The following command will give you the list of instances of a class in memory and their addresses:

!DumpHeap -type LeakingClass

Then call GCRoot with an address and see what pins the object in memory:

!GCRoot <your address here>

You can copy paste the address from the results of DumpHeap command.

evpo
  • 2,436
  • 16
  • 23