Question

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?

Was it helpful?

Solution

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...
}

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top