【发布时间】:2016-02-22 10:28:47
【问题描述】:
今天我注意到我制作的一个小程序在程序生命周期的前 10~20 秒内经常触发 GC。之后它几乎不会再次触发。
在此期间仅运行 1 个函数,即以下函数。获取约 2k 的文件路径,并过滤掉其中的大部分。
public static string[] FilterFiles(string path)
{
// Fetch the files from given directory
var files = Directory.GetFiles(path);
// Delete all files that are to small
foreach (string file in files)
{
string fullFile = default(string);
try
{
fullFile = File.ReadAllText(file);
}
catch
{
continue;
}
if (fullFile.Length < Settings.MinimumFileSize)
{
File.Delete(file);
}
}
// Obtain the new list without the small files
List<string> cleanFiles = new List<string>(Directory.GetFiles(path));
List<string> cleanReturn = new List<string>(Directory.GetFiles(path));
// Remove files we have handled before
foreach (string file in cleanFiles)
{
if (File.Exists(Settings.ExtractFolder + "\\" + file.Substring(file.LastIndexOf('\\') + 1) + "_Extract.xml"))
{
cleanReturn.Remove(file);
}
}
return cleanReturn.ToArray();
}
这段时间GC经常触发这个正常吗?
【问题讨论】:
-
它会导致性能下降吗?
-
你操作的字符串有多长?
-
@dotctor 强制 gc 关闭可节省约 2 秒。所以是的。
-
@TheodorosChatzigiannakis 文件路径,
标签: c# garbage-collection