如何克服OutOfMemoryException异常从API拉大

如何克服OutOfMemoryException异常从API拉大

本文介绍了如何克服OutOfMemoryException异常从API拉大的XML文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个API拉1M +的记录。拉工程确定,但试图 ReadToEnd的时到字符串变量我收到一个内存不足的异常。

I am pulling 1M+ records from an API. The pull works ok, but I'm getting an out of memory exception when attempting to ReadToEnd into a string variable.

下面的代码:

        XDocument xmlDoc = new XDocument();

        HttpWebRequest client = (HttpWebRequest)WebRequest.Create(uri);
        client.Timeout = 2100000;//35 minutes
        WebResponse apiResponse = client.GetResponse();

        Stream receivedStream = apiResponse.GetResponseStream();
        StreamReader reader = new StreamReader(receivedStream);

        string s = reader.ReadToEnd();



堆栈跟踪:

Stack trace:

at System.Text.StringBuilder.ToString()
at System.IO.StreamReader.ReadToEnd()
at MyApplication.DataBuilder.getDataFromAPICall(String uri) in
    c:\Users\RDESLONDE\Documents\Projects\MyApplication\MyApplication\DataBuilder.cs:line 578
at MyApplication.DataBuilder.GetDataFromAPIAsXDoc(String uri) in
c:\Users\RDESLONDE\Documents\Projects\MyApplication\MyApplication\DataBuilder.cs:line 543

我能做些什么来解决此问题?

What can I do to work around this?

推荐答案

的是去当内存是一个问题的方式。它也是最快

XMLReader is the way to go when memory is an issue. It is also fastest.

这篇关于如何克服OutOfMemoryException异常从API拉大的XML文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:48