我正在研究C#。我在使用OutOfMemoryException
时获得string.replace(dt,"")
,甚至在使用stringbuilder.replace(dt,"")
时获得{{1}}。我可以知道如何克服这个问题吗?或者以其他方式做同样的事情?
答案 0 :(得分:1)
由于您的数据太大,您不应该尝试同时对其进行操作。而是读取chucks,处理它,然后将其写入磁盘并继续下一个块。
以下是一些代码(未经测试):
string current = getChunk();
while (current.Length > 0)
{
current = current.Replace(oldValue, newValue);
string toSave = current.Substring(0, current.Length - oldValue.Length);
saveToFile(toSave);
current = current.Substring(current.Length - oldValue.Length) + getChunk();
}
我没有保存最后一个oldValue.Length,因为替换可能在一个块中途,在另一个块中途。注意:该代码中可能存在错误,但它非常接近。
答案 1 :(得分:0)
您的字符串可能太大,内存管理器无法为新字符串找到连续的内存块。
您需要优化程序以实现更有效的内存管理。