因此,我试图读取一堆.mcfunction文件(基本上是.txt),并替换||内部包含的内容。与相应的号码。但是我现在的做法会导致大量的内存使用(例如360mb-> 6gb),因此我需要帮助来修复内存泄漏。这是问题所在:
public void Parse() {
staticIDs sIDs = GameObject.Find("Main Camera").GetComponent<staticIDs>();
functions = Directory.GetFiles(@"downloads\", "*.mcfunction", SearchOption.AllDirectories);
totalFunctions = functions.Length;
curFunction = 0;
foreach (string function in functions)
{
System.Threading.Thread.Sleep(100);
string text = File.ReadAllText(function);
if (text.Contains("|"))
{
Debug.Log("Things to parse");
goto contParse;
}
else {
Debug.Log("No things to parse");
goto finishParse;
}
contParse:
lines = File.ReadAllLines(function);
int lineNumber = 0;
foreach (string line in lines)
{
System.Threading.Thread.Sleep(100);
parts = line.Split('|');
temp = "";
int curLine = lineNumber + 1;
foreach (string part in parts)
{
System.Threading.Thread.Sleep(100);
int IDchecked = 0;
done = false;
tryAgain:
if (IDchecked < sIDs.IDnames.Capacity && sIDs.IDnames[IDchecked] == part)
{
temp = temp + sIDs.IDnumbers[IDchecked].ToString();
goto replaced;
}
else if (IDchecked <= sIDs.IDnames.Capacity)
{
IDchecked++;
goto tryAgain;
}
else if (IDchecked > sIDs.IDnames.Capacity)
{
temp = temp + part;
goto replaced;
}
else
{
goto replaced;
}
replaced:
Debug.Log(temp);
}
done = false;
newLines.Add(temp);
}
File.Delete(function);
File.Create(function).Dispose();
foreach (string line in newLines)
{
File.AppendAllText(function, line);
}
finishParse:
curFunction++;
if (curFunction > totalFunctions)
goto move;
}
move:
Debug.Log("got to moving");
Invoke("Move", 10f);
}
我希望将内存使用量降到最低。
答案 0 :(得分:0)
内存泄漏很可能是由字符串连接引起的。 string + string
创建一个全新的(不变的)string
。避免这些,而改用StringBuilder
而不是进行temp = temp + something
,而是将温度设为StringBuilderer
并致电temp.Append(something)