I'm getting an SystemOutOfMemoryException
when creating an Array. Yet the length
of my array does not
exceed Int32.MaxValue
.
This is the code (please don't judge the code, its not my code an at least 7 years old)
Dim myFileToUpload As New IO.FileInfo(IO.Path.Combine(m_Path, filename))
Dim myFileStream As IO.FileStream
Try
myFileStream = myFileToUpload.OpenRead
Dim bytes As Long = myFileStream.Length //(Length is roughly 308 million)
If bytes > 0 Then
Dim data(bytes - 1) As Byte // OutOfMemoryException is caught here
myFileStream.Read(data, 0, bytes)
objInfo.content = data
End If
Catch ex As Exception
Throw ex
Finally
myFileStream.Close()
End Try
According to this question "SO Max Size of .Net Arrays" and this question "Maximum lenght of an array" the maximum length is 2,147,483,647 elements Or Int32.MaxValue
And the maximum size
is 2 GB
So my total length of my array is well within the limits
( 308 million < 2 billion) and also my size is way smaller
then that 2 GB presented (filesize is 298 mb).
Question:
So my question, with regards to arrays what else could cause a MemoryOutOfMemoryException
?
Note: For those wondering the server still has some 10gb free ram space
Note 2: Following dude's advice I monitored the amount of GDI-Objects on several runs. The process itself never exceeds the count 1500 objects.