Results 1 to 6 of 6

Thread: out of memory... weird problem help pls!

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    2

    Question out of memory... weird problem help pls!

    hi guys, hope someone can help me.
    i got a webcam which takes 2 snapshots per second (every 500ms take the snapshot) and overwriting the same file .jpg
    i got a algorithm which get the image from file every 800milliseconds.
    sometimes less than one minute the error out of memory shows.
    I figured out that the memory is not actually out, since i was watching the memory available in task manager.
    I tried to work this algorithm without the webcam, i mean reading the same image every 800ms and it never run out of memory.

    Dim fileName as string
    Dim pimage as Image;
    fileName="c:\photo002.jpg";
    if (File.Exist(fileName))
    pimage=Image.FromFile(fileName); 'out of memory line (i did only a try catch with this line in specific and the problem shows here)
    ...do something
    pimage.dispose();
    End if


    Thanks for help!

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: out of memory... weird problem help pls!

    maybe you should try saving your pictures in a sequence, and renaming them that way to avoid a conflict..

    what I mean is dont overwrite the same image every time you take a picture, take pic1 pic2 pic3, etc... and when you hit 10 maybe reset to 1...

    or does the interface you have to the camera not allow you to specify the file name

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: out of memory... weird problem help pls!

    I believe where Kleinma is headed is that there may be a conflict between reading and writing. Out of memory does not seem to be the error you should be getting, but it does seem fairly likely that you would encounter read/write conflicts.

    I think Kleinma's solution is a pretty good one, but I wonder if even that would work. After all, how would you know which picture is being written to, and which one is safe to read from. Considering the pace at which you are reading and writing, it seems like you should definitely be doing something like that, but I think there is no way to prevent a conflict from arising eventually, which means that the best you can do would be to read as fast as possible, and trap the inevitable (though occasional) error.

    Alternatively, if you have control over the writing, you might multitask the reading and writing using a mutex on the file. Since that seems unlikely, consider this alternative:

    Instead of reading the file directly, how about renaming it. I would expect that this should be far faster than the read itself (fewer bytes moved). You could then read the renamed file without fear of a conflict. This would minimize the chance for conflict.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    2

    Re: out of memory... weird problem help pls!

    I got this is from the MSDN

    Remarks
    The file remains locked until the Image object is disposed.

    If the file does not have a valid image format or if GDI+ does not support the pixel format of the file, this method throws an OutOfMemoryException exception.

    The error occurs when my system try to open the image file while the webcam software is writing (like not 100% of the file), so is there any condition IF to prevent this?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: out of memory... weird problem help pls!

    Unfortunately, I can't look that up at the moment, but I imagine that if it was obvious or even simple, you would have found it. It does seem to me that I saw a CanRead property on a stream reader, so you might be able to get around the problem by testing the file with a streamreader.

    At worst you can trap the error and try again. Put the reading in a function, trap the error in the function, and return a boolean for success or failure. The calling function would then have a loop. This solution is pretty bad, because at the rate your camera is firing, the time taken for the exception would cost you an image or two extra.
    My usual boring signature: Nothing

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: out of memory... weird problem help pls!

    i think you will still have an issue doing this every .5 seconds... there just might not be enough time for the system to do everything it needs to do the way you have it setup now...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width