Out of memory eventhough Garbage collection is in the code

V

Victory

Hi,
After calling this function 190 times, i get an exception
at the point of exception. The stack trace says:
StackTrace " at
System.Drawing.Image.FromFile(String filename, Boolean
useEmbeddedColorManagement) at
Procédo.MainApp.QualityCheckClass.EvaluateTiffImage(ThreadM
essageClass& displayObject, String fileName, String
startingPageNum, String fileExt) in
D:\src\Procédo\MainForm\QualityCheck.vb:line 3957"
String

The code is:

Private Function EvaluateTiffImage(ByRef displayObject
As ThreadMessageClass, ByVal fileName As String, _

ByVal startingPageNum As String, ByVal fileExt As String)
As Boolean

Dim result As Boolean = False

' count of images in the tiff file
Dim imageIndex As Integer = 0

Dim propertyValues As Byte() = Nothing
Dim compressionString As String = ""
Dim compressionValue As Int16 = 0
Dim propertyItems As
System.Drawing.Imaging.PropertyItem()

displayObject.TargetWindow =
DISPLAY_ID.LIST_PROGRESS

Dim tiffData As TiffProcessingDataClass =
displayObject.MessageData

Try

' using the Imaging Managed objects. The Using
statement automatically handles clean up of the tiffImage
object
Using tiffImage As Image =
Image.FromFile(fileName, False)

If Not (tiffImage Is Nothing) Then

propertyItems = tiffImage.PropertyItems

For propertyIndex As Integer = 0 To
propertyItems.Length

' Compression property has an id of
259
If
propertyItems(propertyIndex).Id() = 259 Then

' The value of which is mapped
to a short type (i.e. Int16)
propertyValues =
propertyItems(propertyIndex).Value

' most significant bit is at
higher index
compressionString =
propertyValues(1) & propertyValues(0)
compressionValue =
Convert.ToInt16(compressionString)

tiffData.ImageCompression =
compressionValue

' tif images must have Group4
compression and jpg images must have huffman compression
If ((compressionValue =
IMAGE_COMPRESSION.COMPRESSION_CCITTFAX4) And (fileExt =
".tif")) Or _
((compressionValue =
IMAGE_COMPRESSION.COMPRESSION_HUFFMAN_CCITTRLE) And
(fileExt = ".jpg")) Then

result = True
displayObject.MessageText =
"...Found " & GetCompressionName(compressionValue) & "
Compression for: <" & fileName & ">."

Else
displayObject.MessageText =
"...Error: " & GetCompressionName(compressionValue) & "
Compression: <" & fileName & ">."
displayObject.IsError =
True
End If
DisplayMessage(displayObject)

' found what we are looking for
Exit For
End If
Next

Application.DoEvents()

' release the reference to the image
object
imageIndex += 1

Else

displayObject.MessageText = "...Error:
GDI+ error was unable to create a document for <" &
fileName & ">."
displayObject.IsError = True
DisplayMessage(displayObject)

End If
End Using
Catch ex As Exception

Dim innerExceptionString As String = Nothing

If ex.InnerException Is Nothing Then
innerExceptionString = "No inner exception"
Else
innerExceptionString =
ex.InnerException.InnerException.Message
End If

displayObject.MessageText = "An exception
occurred: <" & ex.Message & ">, this error happened when "
& _

"Procedo was trying to process file: <" & fileName & ">
inner exception: <" & _

innerExceptionString & ">."
displayObject.IsError = True
displayObject.TargetWindow =
DISPLAY_ID.LIST_RESULT
DisplayMessage(displayObject)

End Try

GC.Collect()
GC.WaitForPendingFinalizers()

Return result
End Function

Thanks for any help,
Mars
 
G

Göran Andersson

Victory said:
Hi,
After calling this function 190 times, i get an exception
at the point of exception. The stack trace says:
StackTrace " at
System.Drawing.Image.FromFile(String filename, Boolean
useEmbeddedColorManagement) at
Procédo.MainApp.QualityCheckClass.EvaluateTiffImage(ThreadM
essageClass& displayObject, String fileName, String
startingPageNum, String fileExt) in
D:\src\Procédo\MainForm\QualityCheck.vb:line 3957"
String

Don't call GC.Collect in your code. There is nothing in your code that
gets better from it. The garbage collector will perform a collection
when needed.

Have you tried to load only the specific file where the error occurs?
It's possible that the error comes from a corrupt file instead of an
actual lack of memory.
 
V

Victory

Goran,
Yes, you are correct, that file causes the exception. How
can i know that the file is corrupt when the exception
returns "Out of memory"? Any ideas?
thanks,
Mars
 
V

Victory

James,
In general the isolation method you described works. In
this case however, the Image.LoadFrom is the cause of the
exception.
Thank you anyways,
Mars
 
J

James Hahn

There's nothing special about this case. If the error occurs at that line
you know that the file must be corrupt.
 
J

James Hahn

Yes. 'Out of memory' almost always means 'corrupted' for a TIF file. It's a
characteristic of the data structures used that the decoding algorithms are
generally unable to determine when they are using invalid data. and simply
continue to issue requests for more memory. Garbage collection won't help,
as the memory appears to be in use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top