Read large binary file into byte array - File.ReadAllBytes throws 'System.OutOfMemoryException' RRS feed

  • Question

  • User50233863 posted

    Running on Windows Server 2003, IIS 6.0, Visual Studio 2010, .Net Framework 3.5

    The web service is throwing a "System.OutOfMemoryException" whenever it tries to read a file large binary file (> 80 Meg).

    To make troubleshooting this issue easier, I have separated out the code into a simple web service with only one call
    GetByteArrayFromFile which reads a file and returns a byte array.

    It consistently throws the 'Out of Memory' exception with files larger than 80 Meg.

    Are there any steps I can take to resolve this issue?

    'Web Service Call
    Imports System.Web.Services, System.Web.Services.Protocols, System.ComponentModel

    <System.Web.Services.WebService(Description:="Testing", _    
      Namespace:="http://Testing.org/")> _
      <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
      <ToolboxItem(False)> _
    Public Class ProcSvcs   Inherits System.Web.Services.WebService

    <WebMethod(Description:="Byte array from File")> _  
    Public Function GetByteArrayFromFile(ByVal PathFName As String) As Byte()    
      Dim Content() As Byte = System.IO.File.ReadAllBytes(path:=PathFName)      
      Return Content    
    Catch ex As Exception      
       Throw New Exception(ex.Message)    
    End Try  
    End Function

    End Class


    Tuesday, June 27, 2017 10:40 PM

All replies

  • User991499041 posted

    Hi Tim_Shaf,

    The web service is throwing a "System.OutOfMemoryException" whenever it tries to read a file large binary file (> 80 Meg).

    There are lots of things can cause OutOfMemoryException, you server may be in low resource but high usage.

    You can enable WCF tracing on client side to see execution information and detailed exception.

    Enable WCF tracing.




    Wednesday, June 28, 2017 3:06 AM
  • User753101303 posted


    ex.ToString() might give some more information? The while file is read into memory and then likely serialized so I expect this to fail though I find a bit surprising it fails already with 80 Mb.

    Dépends what you want to do. For example if serving images you could just return a link and the link would serve the file "as usual" (possibly using a ASHX handler that would stream the file content rather than to load it all in memory).

    Not directly related but from a security point of view it seems this method allows to retrieve just any file from your web server. Always use a "white list" approach ie make sure to server only what you really do want to expose...

    Tuesday, August 1, 2017 2:27 PM