【发布时间】:2013-10-09 06:32:13
【问题描述】:
基本上,我从 SQL Server 获取数据。如果我选择所有数据(大约 80000 行),那么我会收到以下错误(接收 100 行工作正常)
Exception of type 'System.OutOfMemoryException' was thrown. <br/>
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <br/>
Exception Details: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
Stack Trace:
[OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.]
System.Text.StringBuilder.ToString() +29
System.IO.StringWriter.ToString() +14
System.Net.WebUtility.HtmlEncode(String value) +110
System.Web.Util.HttpEncoder.HtmlEncode(String value) +54
System.Web.UI.HtmlControls.HtmlContainerControl.set_InnerText(String value) +24
PerformanceCompare._Default.Page_Load(Object sender, EventArgs e) in C:\Users\KK33562\Documents\Visual Studio 2010\Projects\TestWebService\PerformanceCompare\Default.aspx.cs:34
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
Web.config(客户端)
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="endpointbehaviour">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" closeTimeout="00:50:00"
openTimeout="00:50:00" receiveTimeout="00:50:00" sendTimeout="00:50:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53268/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="WCFService.IService1"
name="BasicHttpBinding_IService1" behaviorConfiguration="endpointbehaviour" />
</client>
</system.serviceModel>
Web.config(服务器端)
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="TestWCF.Service1Behavior">
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="TestWCF.Service1" behaviorConfiguration="TestWCF.Service1Behavior">
<endpoint address="" binding="basicHttpBinding" contract="TestWCF.IService1" bindingConfiguration="BasicHttpBinding_IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" openTimeout="00:50:00" closeTimeout="00:50:00" sendTimeout="00:50:00" receiveTimeout="00:10:00">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
【问题讨论】:
-
看起来很简单:“当没有足够的内存来继续执行程序时抛出的异常”msdn.microsoft.com/en-us/library/…
-
@DaveZiegler 感谢您的回复!我知道我在内存管理方面有问题。我的问题是我该如何解决这个问题..
-
嗯,你要返回多少数据?有多少内存可用?我不认为这是您可以在 web.config 中解决的问题。您可以带回更少的数据,还是一次需要全部 80000 多行?
-
除非您只通过快速的本地网络访问它,否则您必须考虑性能是您不应该尝试一次返回所有数据的另一个原因。
-
那么,有没有办法一次返回 80000+ 行?我应该分解我的代码多次调用吗?我对内存管理一无所知....
标签: wcf serialization deserialization out-of-memory