在我上传文件到FTp网站的vb应用程序中,我使用大尺寸数组。在声明数组期间,有时会出现Exception of type 'System.OutOfMemoryException' was thrown.
我已将数组声明为
Const BufferSize As Integer = 400000000
Dim content(BufferSize - 1) As Byte
实际上400,00000将达到380 MB左右。我有4 GB RAM,使用率不高。为什么有时会显示此错误?如何解决?
答案 0 :(得分:0)
对我之前的same question和answer进行了处理(感谢Aik对他的出色解释)。
即使你有4gb ram,也无法知道你有380mb的可用空间。如果RAM的碎片太大,可能会导致问题。
您需要查看数据以克服此问题(请参阅此answer ...)
AIK的解释:
例如,在我的机器上,我不能分配超过908 000 000 一个数组中的字节数,但我可以分配100 * 90 800 000而不需要任何数据 如果它存储在更多数组中的问题:
Dim toBigArray As Byte() = New Byte(908000000) {}
'doesn't work (6 zeroes after 908)
' allocation in more arrays
Dim a As Byte()() = New Byte(100)() {}
For i As Integer = 0 To a.Length - 1
' it works even there is 10x more memory needed than before
' (5 zeroes after 908)
a(0) = New Byte(90800000) {}
Next
答案 1 :(得分:0)
使用这样大的阵列是一个糟糕的主意。您可以在小块中流式传输文件。以下是此link
的一些示例代码''' <summary>
''' Methods to upload file to FTP Server
''' </summary>
''' <param name="_FileName">local source file name</param>
''' <param name="_UploadPath">Upload FTP path including Host name</param>
''' <param name="_FTPUser">FTP login username</param>
''' <param name="_FTPPass">FTP login password</param>
Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String, ByVal _FTPUser As String, ByVal _FTPPass As String)
Dim _FileInfo As New System.IO.FileInfo(_FileName)
' Create FtpWebRequest object from the Uri provided
Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
' Provide the WebPermission Credintials
_FtpWebRequest.Credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)
' By default KeepAlive is true, where the control connection is not closed
' after a command is executed.
_FtpWebRequest.KeepAlive = False
' set timeout for 20 seconds
_FtpWebRequest.Timeout = 20000
' Specify the command to be executed.
_FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
' Specify the data transfer type.
_FtpWebRequest.UseBinary = True
' Notify the server about the size of the uploaded file
_FtpWebRequest.ContentLength = _FileInfo.Length
' The buffer size is set to 2kb
Dim buffLength As Integer = 2048
Dim buff(buffLength - 1) As Byte
' Opens a file stream (System.IO.FileStream) to read the file to be uploaded
Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()
Try
' Stream to which the file to be upload is written
Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()
' Read from the file stream 2kb at a time
Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)
' Till Stream content ends
Do While contentLen <> 0
' Write Content from the file stream to the FTP Upload Stream
_Stream.Write(buff, 0, contentLen)
contentLen = _FileStream.Read(buff, 0, buffLength)
Loop
' Close the file stream and the Request Stream
_Stream.Close()
_Stream.Dispose()
_FileStream.Close()
_FileStream.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
HOW TO USE:
' Upload file using FTP
UploadFile("c:\UploadFile.doc", "ftp://FTPHostName/UploadPath/UploadFile.doc", "UserName", "Password")
编辑1:
尝试这些链接可能对您有帮助。