1

i use foreach to read all the image from a folder

        string[] filePaths = Directory.GetFiles(Workspace.InputFolder, "*.*");
        foreach (string imageFile in filePaths)
        {
            // Some Process here, the output are correct, just after output 
               the error happen
        }

But it come out error

System.OutOfMemoryException was unhandled
  Message=Out of memory.
  Source=System.Drawing 

Is the problem cause by the foreach loop keep looping after the process finish? What should i do for releasing the memory? Thanks.

5
  • 3
    It depends greatly on what you are doing with each filename. If you are opening a file, make sure and Dispose() the objects when you are done. Commented Nov 14, 2011 at 19:53
  • Can you describe what you do inside the loop? Commented Nov 14, 2011 at 19:54
  • You need to post more code, specifically what you're doing inside the foreach loop. My gut feeling is that you're creating disposable object instances and not disposing of them correctly, but it's not possible to know for sure unless you post some more code. Commented Nov 14, 2011 at 19:55
  • i use the image to do OCR that convert the image into digital data. Commented Nov 14, 2011 at 19:55
  • 1
    So you found the source of memory leakage Commented Nov 14, 2011 at 19:56

1 Answer 1

6

Given your exception, it looks like you're working with objects in the System.Drawing namespace.

If you are opening and manipulating an image in your foreach loop, for example, make sure that you call Dispose() to release the images resources as soon as you're done with them. You can, alternatively, wrap this in a using statement, ie:

    foreach (string imageFile in filePaths)
    {
        using (var image = Image.FromFile(imageFile)
        {
            // Use the image...
        } // Image will get disposed correctly here, now.
    }

Be aware that it's not just images that are potentially the problem, but any resource that implements IDisposable. Many of the classes in System.Drawing are disposable - make sure that you either access them as above (via using) or call Dispose() on them when done.

Sign up to request clarification or add additional context in comments.

4 Comments

i did try your solution but same error message come out, how should i call the Dispose() function?
@Xuan You'd need to call the Dispose() method on all objects that include it, once you've finished using them, inside of your foreach loop. This is going to include any images, but also potentially your OCR classes, etc.
@ReedCopsey Thanks for helping, i solve the problem by change the . to specific image file(*.tif).
Xuan, simply specifying "*.tif" isn't going to fix the root issue. The problem at hand is that you almost certainly are working with Disposable types, and aren't properly disposing of the instances when you're done processing them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.