以最少的内存使用量将数据从 SQLDataReader 保存到 FileStream
我使用yield 语句将SQLDataReader 包装为IEnumberable。我想用它转储到文件。我发现内存利用率相当高。想知道是否有人对如何以最小或设置内存利用率来做到这一点有任何想法。我不介意指定一个缓冲区,我只是想知道在毫无戒心的服务器上释放它之前它会是什么。
我一直在使用类似以下的东西:
class Program
{
static void Main(string[] args)
{
var fs = File.Create("c:\\somefile.txt");
var sw = new StreamWriter(fs);
foreach (var asdf in Enumerable.Range(0, 500000000))
{
sw.WriteLine("adsfadsf");//Data from Reader
}
sw.Close();
}
}
string commandText = @"SELECT name FROM {0} WHERE name NOT LIKE '%@%'";
SqlCommand sqlCommand = new SqlCommand(string.Format(commandText, list.TableName.SQLEncapsulate()),
_connection);
using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
{
while (sqlDataReader.Read())
{
yield return sqlDataReader["name"].ToString();
}
}
I've wrapped a SQLDataReader as an IEnumberable using a yield statement. I'd like use this to dump to a file. I'm seeing some pretty heavy memory utilization. Was wondering if anyone had any ideas on how to do this with minimal or set memory utilization. I don't mind specifying a buffer I'd just like to know what it'll be before I unleash this on an unsuspecting server.
I've been using something like the following:
class Program
{
static void Main(string[] args)
{
var fs = File.Create("c:\\somefile.txt");
var sw = new StreamWriter(fs);
foreach (var asdf in Enumerable.Range(0, 500000000))
{
sw.WriteLine("adsfadsf");//Data from Reader
}
sw.Close();
}
}
string commandText = @"SELECT name FROM {0} WHERE name NOT LIKE '%@%'";
SqlCommand sqlCommand = new SqlCommand(string.Format(commandText, list.TableName.SQLEncapsulate()),
_connection);
using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
{
while (sqlDataReader.Read())
{
yield return sqlDataReader["name"].ToString();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些大的内存吞吐量不是问题,并且在处理大量数据时是不可避免的。
您读取的数据将作为新对象分配在堆上,但它们是短暂的对象,因为您只是读取数据,写入数据,然后将其丢弃。
.NET 中的内存管理不会尝试将内存使用量保持在尽可能低的水平,因为拥有大量未使用的内存并不会让计算机变得更快。当您创建和释放对象时,它们只会在堆上被遗弃一段时间,垃圾收集器最终会清理它们。
当您进行一些繁重的数据处理时,.NET 应用程序使用大量内存是正常的,并且一段时间后内存使用量会再次下降。如果系统的其他部分需要内存,垃圾收集器将进行更积极的收集以释放尽可能多的内存。
Some heavy memory throughput is not a problem, and is unavoidable when you process a lot of data.
The data that you read will be allocated as new objects on the heap, but they are short lived objects as you just read the data, write it, then throw it away.
The memory management in .NET doesn't try to keep the memory usage as low as possible, as having a lot of unused memory doesn't make the computer faster. When you create and release objects, they will just be abandoned on the heap for a while, and the garbage collector cleans them up eventually.
It's normal for a .NET application to use a lot of memory when you are doing some heavy data processing, and the memory usage will drop again after a while. If there is some other part of the system that needs the memory, the garbage collector will do a more aggressive collection to free up as much as possible.