一次给出例外,另一次则没有
我使用多线程将文件复制到另一个地方。我从流中需要字节数组并处理流。在这个例子中,我使用 7 个线程来复制 3GB 文件。第一个线程可以获取字节数组,但在第二个线程发生异常“System.OutOfMemoryException” '
public void Begin()
{
FileStream stream = new FileStream(pathToFile, FileMode.Open);
stream.Position = (threNmb - 1) * 536870912;
BinaryReader reader = new BinaryReader(stream);
for (long i = 0; i < (length); i++)
{
source.Add(reader.ReadByte());//gives exception at i=134217728
}
reader.Dispose();
reader.Close();
stream.Dispose();
stream.Close();
}
I use multi threading to copy file to another place.I from stream needed byte array and dispose stream.at this example i use 7 threads to copy 3gb file.1st thread can get byte array,but at 2nd thread occurs exception 'System.OutOfMemoryException'
public void Begin()
{
FileStream stream = new FileStream(pathToFile, FileMode.Open);
stream.Position = (threNmb - 1) * 536870912;
BinaryReader reader = new BinaryReader(stream);
for (long i = 0; i < (length); i++)
{
source.Add(reader.ReadByte());//gives exception at i=134217728
}
reader.Dispose();
reader.Close();
stream.Dispose();
stream.Close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在使用
List
。这将是一种非常低效的数据复制方式,并且您可能会通过使用多个线程来降低其效率。此外,如果您使用多个线程中的单个列表,则您的代码已经损坏 -List
不是线程安全的。即使是这样,您也会混合来自不同线程的数据,因此您将无法重新组合原始数据。哦,如果不使用using
语句,如果抛出异常,您将使文件句柄保持打开状态。换句话说,我建议你彻底放弃目前的做法。相反,将文件从一个位置复制到另一个位置(假设您不能使用
File.Copy
出于某种原因),基本上应该是这样的情况:using
语句)不需要将所有内容都存储在内存中。请注意,在 .NET 4 中,使用 可以使这变得更加容易
Stream.CopyTo
方法。它为您执行第三步和第四步。It looks like you're using a
List<byte>
. That's going to be a very inefficient way of copying data, and you're probably making it less efficient by using multiple threads. Additionally, if you're using a single list from multiple threads, your code is already broken -List<T>
isn't thread-safe. Even if it were, you'd be mixing the data from the different threads, so you wouldn't be able to reassemble the original data. Oh, and by not usingusing
statements, if an exception is thrown you're leaving file handles open. In other words, I'm advising you to completely abandon your current approach.Instead, copying a file from one place to another (assuming you can't use
File.Copy
for some reason), should basically be a case of:using
statements)There's no need to have everything in memory. Note that in .NET 4 this is made even easier with the
Stream.CopyTo
method. which does the third and fourth steps for you.