使用C#在asp.net中填充DataTable时发生System.OutOfMemoryException错误 [英] System.OutOfMemoryException error occur when filling DataTable in asp.net using C#

查看:71
本文介绍了使用C#在asp.net中填充DataTable时发生System.OutOfMemoryException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有超过1000000条的大记录,我想将此数据填充到DataTable中,但是在填充DataTable时,它会抛出异常,例如 System.OutOfMemoryException .我该如何解决这个错误?另外,我有4 GB RAM和64位操作系统.

I have large record like more then 1 000 000 and I want to fill this data into DataTable, but at time of filling DataTable it is throwing exception like System.OutOfMemoryException. How I can solve this error? Also I have 4 GB RAM and 64-bit operating system.

推荐答案

如果-需要-内存中的所有数据,并且您的应用程序池是x64,并且您使用的是.NET 4.5,则可以使用gcAllowVeryLargeObjects 指令...要针对ASP.NET执行此操作,您需要为应用程序池添加自己的 aspnet.config 文件(请参见

If you -need- all that data in memory, and your app pool is x64, and you are using .NET 4.5, you can use the gcAllowVeryLargeObjects directive... to do this for ASP.NET, you'd need to add your own aspnet.config file for your app pool (see this link for more info) and set this configuration for startup:

<configuration>
  <runtime>
    <gcAllowVeryLargeObjects enabled="true" />
  </runtime>
</configuration>

也就是说,一次性处理内存中如此大的 DataTable 的可能性更大,特别是对于Web应用程序(通常适用于多个用户)而言.

That said, handling such a large DataTable in memory all at once is more likely not the way to go, specially for web applications (which will generally work for more than one user).

拥有这么多的内存(可能每个用户),尽管您很可能在任何给定的时间仅处理该内存的一小部分,但这只是浪费,您可能应该寻找一种不同的处理方式问题,而不是强行使用.NET允许在内存中容纳更大的对象.

Having that much memory occupied (per user, probably), while you more likely only work on a small subset of that memory at any given time, is just a waste, and you should probably be looking at a different way of handling the problem, rather than bruteforcing .NET to allow for larger objects in memory.

这篇关于使用C#在asp.net中填充DataTable时发生System.OutOfMemoryException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆