【发布时间】:2015-10-16 09:59:22
【问题描述】:
在 C# 中,如果我有 4-5 GB 的数据现在以 bytes 的形式出现,那么我将其转换为 string 那么这对内存有什么影响以及如何在对大字符串使用变量时进行更好的内存管理?
代码
public byte[] ExtractMessage(int start, int end)
{
if (end <= start)
return null;
byte[] message = new byte[end - start];
int remaining = Size - end;
Array.Copy(Frame, start, message, 0, message.Length);
// Shift any remaining bytes to front of the buffer
if (remaining > 0)
Array.Copy(Frame, end, Frame, 0, remaining);
Size = remaining;
ScanPosition = 0;
return message;
}
byte[] rawMessage = Buffer.ExtractMessage(messageStart, messageEnd);
// Once bytes are received, I want to create an xml file which will be used for further more work
string msg = Encoding.UTF8.GetString(rawMessage);
CreateXMLFile(msg);
public void CreateXMLFile(string msg)
{
string fileName = "msg.xml";
if (File.Exists(fileName))
{
File.Delete(fileName);
}
using (File.Create(fileName)) { };
TextWriter tw = new StreamWriter(fileName, true);
tw.Write(msg);
tw.Close();
}
【问题讨论】:
-
这里缺少很多的上下文。
-
我只是想知道字节和字符串之间的内存管理,所以我没有显示任何代码。如果您有知识分享,请不要投反对票
-
这是您需要的第一个知识点:这取决于语言。
-
@TheParamagneticCroissant:你是否也在对我的其他问题投反对票??????你看起来像一个生病的伙伴。冷静一下:)
-
@IgnacioVazquez-Abrams :谢谢 :) 我已经更新了我的问题中的语言。它的c#
标签: c# memory-management