painiyff
painiyff

Reputation: 2719

How to send files without worrying about memory fragmentation?

I have a simple VB.NET web application that allows users to download particular files on the server's hard drive. However, some of these files are extremely large, up to 1GB in size. Sometimes when the web application tries to send these files, the application craps out and throws a System.OutOfMemoryException. After some research, I found out this is due to memory fragmentation, or, there is not enough continuous memory to allocate for the entire file.

Is there any way at all that I can avoid this error from occurring? Our server has enough physical memory overall to allocate for downloading these large files, it is simply an issue of memory fragmentation (from what I have read on the Internet anyways).

For reference, here's the code that is currently in place:

Public Function SendFile(ByVal fileName As String, ByVal contentType As String, ByVal fileLocation As String) As ActionResult
    Dim fileBytes as Byte() = New WebClient().DownloadData(fileLocation)
    Return File(fileBytes, contentType, fileName)
End Function

... where fileName is the name of the file, contentType is the MIME type, and fileLocation is the physical location on disk.

Any suggestions?

Upvotes: 0

Views: 78

Answers (1)

ThatGuy
ThatGuy

Reputation: 228

You could limit the transfer size to 10mb maybe. Dedicate the first few bytes of transferred data to contain information about the file being transferred including Start byte, end byte, transaction ID (if you choose to do so) etc..

The server analyses the file and determines things like, the number of transaction needed to send the file in Z=(10mb - dedicated space) chunks. It the reads the file in from position x(0) to y(Z-1) and reports those positions in the dedicated space, sends chunk to client, advances x to =y+1 and restarts the loop.

The client would create a blank file on disk, request file from server, receive the chuck and write it to the file disk at the positions which are contained in the dedicated space.

Upvotes: 0

Related Questions