File stream holds resource and due to this OutOfMemoryException occur. RRS feed

  • Question

  • Below is my code by which getting Byte() from file. This code is working fine.

    Issue is that,  br.ReadBytes(fs.Length) when this statement runs, it holds memory equals to size of read file. Now I need to free this memory after completion of this task because when I am try to attached second file it is giving error of "outofmemory".

    Could please help me on it.


    When I run my application, it take 107 MB size in memory, below is the screen shot of task manager.

    Size of application before file attachment

    Now when I attached file of size 342 MB then size of my application become 452 MB after executing this line

    br.ReadBytes(fs.Length) , below is the image of task manager

    So I need to free this memory of 352 MB after completion of this function, but this memory is not clear, could you please help me on it.

    Public Shared Function GetBytesFromFile(ByVal p_strPath As String) As Byte()            
    Dim mybyte() As Byte        
    Using fs As System.IO.FileStream = New FileStream(p_strPath, FileMode.Open, FileAccess.Read)                       
    Using br As BinaryReader = New BinaryReader(fs)                   
    mybyte = br.ReadBytes(fs.Length) ' need to free this memory                         
    End Using        
    End Using                                      
    Return mybyte        
    Catch ex As OutOfMemoryException            
    Dim a As New OutOfMemoryException            
    Throw a        
    Catch ex As Exception            
    ' Log the error               
     End Try    
    End Function

    Tuesday, November 29, 2016 11:02 AM

All replies

  • Maybe the consumption is caused by the mybyte array, which is returned, then kept excessively in other parts of your program. Show some details about further processing of the bytes.

    By the way, in order to read the data easier, consider File.ReadAllBytes.

    Tuesday, November 29, 2016 7:02 PM
  • Hi Manoi,

    Using fs As System.IO.FileStream = New FileStream(p_strPath, FileMode.Open, FileAccess.Read)                       
    Using br As BinaryReader = New BinaryReader(fs)                   
    mybyte = br.ReadBytes(fs.Length) ' need to free this memory 

    Based on your code shows, reads all content of the file into a contiguous region in memory.  It's not surprising that you get an out-of-memory condition.

    If you want to read a file completely, it's much simpler than the ReadFully method you use.  To read the raw bytes of a stream use do something like this:

    using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) 
       byte[] buffer = new byte[fs.Length];
       int bytesRead = fs.Read(buffer, 0, buffer.Length);
       // buffer now contains the entire contents of the file

    If you want VB sample, please also refer to https://msdn.microsoft.com/en-us/library/system.io.stream.read(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Kristin Xie Wednesday, November 30, 2016 7:04 AM
    Wednesday, November 30, 2016 7:03 AM