我有一个对象列表,列表中的对象数量约为506,011个。
它在内存中占用了190MB。
一段时间后,我不再需要这个列表。我清空了列表。
list.clear() 命令可以清除该列表中的所有对象。
但是,我的应用程序仍然占用着190MB 的内存。
如何正确地处理该列表?
public class FileProperty
{
public string Name { get; set; }
public string DirectoryPath { get; set; }
public long LastWriteTime { get; set; }
public long Size { get; set; }
}
void Main()
{
var Sqlite = new SqliteConnection(@"Filename=D:\Work\UserData.db");
var FileProps=Sqlite.Query<FileProperty>("SELECT *FROM FileProperties;").ToList();
//Task completed with that collection
//Now want to free memory
FileProps.Clear();
//But memory not freed
}
GC.Collect(); Thread.Sleep(1);
的组合非常可靠。Thread.Sleep()
确保操作系统可以切换到 GC 线程。 - Thomas Weller