John Brush
John Brush

Reputation: 177

HttpClient download file OutOfMemory Error

Hello i use this code for file downloader function but receive OutOfMemory Exception with my large file size.

private async void DownloadFile()
    {

        string url = "http://download.microsoft.com/download/0/A/F/0AFB5316-3062-494A-AB78-7FB0D4461357/Windows_Win7SP1.7601.17514.101119-1850.AMD64CHK.Symbols.msi";
        string filename = "test.msi";

        HttpClientHandler aHandler = new HttpClientHandler();
        aHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
        HttpClient aClient = new HttpClient(aHandler);
        aClient.DefaultRequestHeaders.ExpectContinue = false;
        HttpResponseMessage response = await aClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead); // Important! ResponseHeadersRead.

        var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
        var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite);

        Stream stream = await response.Content.ReadAsStreamAsync();
        IInputStream inputStream = stream.AsInputStream();
        ulong totalBytesRead = 0;
        while (true)
        {
            // Read from the web.
            IBuffer buffer = new Windows.Storage.Streams.Buffer(1024);
            buffer = await inputStream.ReadAsync(
                buffer,
                buffer.Capacity,
                InputStreamOptions.None);

            if (buffer.Length == 0)
            {
                // There is nothing else to read.
                break;
            }

            // Report progress.
            totalBytesRead += buffer.Length;
            System.Diagnostics.Debug.WriteLine("Bytes read: {0}", totalBytesRead);

            // Write to file.
            await fs.WriteAsync(buffer);

            buffer = null;

        }
        inputStream.Dispose();
        fs.Dispose();
    }

PS. I use httpclient for download but Windows phone background transfer have 100MB limitation on wifi.

Upvotes: 2

Views: 1544

Answers (1)

Martin Costello
Martin Costello

Reputation: 10862

I had this same issue in some different code I was working on last year, though that was with POST not GET. Unfortunately I didn't find a way around it, so instead I resorted to using WebRequest directly. here's a snippet from the fix:

// HttpWebRequest is used here instead of HttpClient as there is no support
// in HttpClient to not buffer uploaded streams, which for our purposes
// causes an OutOfMemoryException to occur.
HttpWebRequest request = WebRequest.Create(ServiceUri.ToString() + requestUri) as HttpWebRequest;
request.ContentLength = content.Length;
request.ContentType = "application/octet-stream";
request.Method = "POST";

request.AllowWriteStreamBuffering = false;  // Prevents OutOfMemoryException

I think your fix is to somehow disable buffering when downloading using HttpClient.

Upvotes: 3

Related Questions