0

I'm having memory issues reading and closing multiple JPG images.


I'm loading JPG files into an image object, reading the file information (size, dimensions, date, etc.), and reading their EXIF data to get the geocode for Lat/Long. Then I'm pushing the information to a ListView object. This is all working great on folders with up to 60 or so JPG's. Once I go past a certain number (not sure what the threshold actually is) I get my favorite error...


************** Exception Text ************** System.OutOfMemoryException: Out of memory. at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) at System.Drawing.Image.FromFile(String filename)

...etc.


I'm leaking memory somewhere, but I'm a bit of a novice with .NET applications. I'm calling this routine from a loop which reads through a collection of JPG files in a directory.

Public Function JPGDat(ByRef jpg As FileInfo) As ListViewItem

    Dim filEntry(6) As String
    Dim lstEntry As ListViewItem = Nothing
    Dim pic As Image = Image.FromFile(jpg.FullName) 'HERE IS WHERE WE CRASH

    filEntry(0) = jpg.Name
    filEntry(1) = (Math.Round(jpg.Length / 1024)).ToString()    'File Size
    filEntry(2) = Format(pic.PhysicalDimension.Width, "0")      'Pixel width dimension
    filEntry(3) = Format(pic.PhysicalDimension.Height, "0")     'Pixel height dimension

    Try
        Dim CLatt As Double = GetCoord(pic.GetPropertyItem(2))  'Get Latitude from EXIF
        Dim CLong As Double = GetCoord(pic.GetPropertyItem(4))  'Get Longitude from EXIF

        filEntry(4) = Format(CLatt, "0.000000000000000")
        filEntry(5) = Format(CLong, "0.000000000000000")
    Catch ex As Exception
        filEntry(4) = ""
        filEntry(5) = ""
    End Try
    filEntry(6) = "Date: " + Format(jpg.CreationTime, "yyyy-MM-dd")

    lstEntry = New ListViewItem(filEntry)
    If filEntry(4) <> "" Then
        lstEntry.Checked = True
    End If
    pic.Dispose()

    Return lstEntry
End Function
  • What memory does Task Manager report your process is using just before it crashes? – Dai Feb 23 '15 at 21:50
  • Just because that is where the OOM exception occurs, doesn't mean that is where the issue is. You will need to profile your application to find the leak. – user1620220 Feb 23 '15 at 21:53
  • My apologies folks. The error was in a corrupt JPG file... not the code. Adding one more Try command solved the problem. – Luke Bingham Feb 23 '15 at 22:10

1 Answers1

0

Make sure to put the open JPG command in a Try structure...

    Try
        pic = Image.FromFile(jpg.FullName)
        filEntry(2) = Format(pic.PhysicalDimension.Width, "0")      'Pixel width dimension
        filEntry(3) = Format(pic.PhysicalDimension.Height, "0")     'Pixel height dimension

    Catch ex As Exception
        MsgBox("Error in file " + jpg.FullName + vbCrLf + ex.Message)
        JPGDat = New ListViewItem(filEntry)

        Exit Function
    End Try

That way, if you get a corrupt JPG, you won't think you have a memory leak!