Results 1 to 3 of 3

Thread: FTP client frontend outofmemoryexception

  1. #1

    Thread Starter
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    FTP client frontend outofmemoryexception

    Hi all,

    I have been working on providing an FTP GUI frontend which users can use to ftp files from a specified directory (usually their local drives) to a server. For the most part my program is working fine however during my testing I have encountered an intermittent error relating to outofmemoryexceptions. The ftp process has completed in the past but for some unknown reason (I havent changed anything) the memory error is occuring. I realise what the error means but Im at a loss as to how to rectify the issue. This always happens when trying to transfer large files but recently started for files around 200MB. I know this may not be the most efficient way of doing this but the files we work with can be quite large and in extreme cases exceed 1GB.

    I have googled and read up on ways to fix this but to no avail, can someone please help me with this error as its got me baffled.

    The error usually occurs on either of the bolded lines.

    There is additional code which I have ommited as it is not necessary.

    Thanks

    Code:
    For Each myFilePath In myFiles
    
                Dim myStream As System.IO.Stream
                Dim arrayFiles As String() = myFilePath.Split("\")
                myFile = arrayFiles(arrayFiles.Length - 1)
                Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://<myserver>/" & myFile), System.Net.FtpWebRequest)
                With request
                    .Credentials = New System.Net.NetworkCredential(txtID.Text, txtPW.Text)
                    .Method = System.Net.WebRequestMethods.Ftp.UploadFile
                    .UsePassive = True
                    .UseBinary = True
                    .KeepAlive = False
                End With
    
                Dim file() As Byte = System.IO.File.ReadAllBytes(myFilePath)
                myStream = request.GetRequestStream()
    
                ftpTrans.Add(myFile)
    
                Try
                    Const defaultChunkLength As Integer = 1024
                    Dim curPos As Integer = 0
        
                    While (curPos < file.Length)
    
                        If ftpFilesBG.CancellationPending Then
                            e.Cancel = "true"
                            Exit For
                        End If
    
                        Dim chunkLength As Integer
                        If (curPos + defaultChunkLength) > file.Length Then
                            chunkLength = file.Length - curPos
                        Else
                            chunkLength = defaultChunkLength
                        End If
                        myStream.Write(file, curPos, chunkLength)
                        updateProgress_ThreadSafe(Me.lblStatus, "Transferring file: " & myFile, 0)
                        curPos += defaultChunkLength
                    End While
    Next

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,292

    Re: FTP client frontend outofmemoryexception

    Do you know what ReadAllBytes means? It means that the whole file, each and every byte, is read into memory in 1 shot.... If you have 1MB file then it takes 1MB of memory space to store the bytes. If you get 1GB file then 1GB of available memory is required to store it in.... No wonder why you run out of memory when trying to transfer a large file... Use a FileStream and read the file in chunks instead...
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Re: FTP client frontend outofmemoryexception

    Yes I do know what it means and I have read to use filestream but wasn't sure how. I'll try and figure it out. Thanks so much for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width