问题描述
我发现了一个 SystemOutOfMemoryException
创建数组时。然而,长度
我的数组不
超过 Int32.MaxValue
。
I'm getting an SystemOutOfMemoryException
when creating an Array. Yet the length
of my array does not
exceed Int32.MaxValue
.
这是在code(请不要判断code,它不是我的C的至少有7年$ C $岁)
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
根据这个问题的.NET数组 SO最大尺寸,这问题:Maximum数组的最大长度lenght是 2,147,483,647元或者Int32.MaxValue
和最大尺寸
是 2 GB
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
所以,我的总我的数组的长度以及内部的限制
(3.08亿2十亿),并我的尺寸是这样小的
则是2 GB presented(文件大小为298 MB)。
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).
问:所以我的问题,关于阵列还有什么可以引起 MemoryOutOfMemoryException
?
Question:So my question, with regards to arrays what else could cause a MemoryOutOfMemoryException
?
注意:对于那些想知道服务器仍然具有一定的10GB可用RAM空间
Note: For those wondering the server still has some 10gb free ram space
注意2:继dude's建议我监控的几个奔跑的图片处理的对象的数量。这个过程本身不会超过计1500的对象。
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.
推荐答案
字节数组是一个字节序列。这意味着你必须分配这么多的内存阵列是一个长度在一个街区。如果你的内存碎片是大非系统无法分配内存,即使您的X GB的内存免费。
byte array is bytes in a sequence. That means you have to allocate so many memory as your array is length in one block. If your memory fragmentation is big than the system is not able allocate the memory even you have X GB memory free.
例如我machyne我不能仅仅分配比在一个阵列中908 000 000个字节,但我可以分配100 * 90 800 000没有任何问题,如果它被存储在多个阵列:
For Example on my machyne I'm not able allocate mere than 908 000 000 bytes in one array, but I can allocate 100 * 90 800 000 without any problem if it is stored in more arrays:
// alocation in one array
byte[] toBigArray = new byte[908000000]; //doesn't work (6 zeroes after 908)
// allocation in more arrays
byte[][] a=new byte[100][];
for (int i = 0 ; i<a.Length;i++) // it works even there is 10x more memory needed than before
{
a[0] = new byte[90800000]; // (5 zeroes after 908)
}
这篇关于SystemOutOfMemoryException当创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!