[英]Memory not releasing .Net Application
我有一個對象列表。 列表中的對象數約為 5,06,011。
它在內存中消耗 190MB。
一段時間后,我不需要那個列表了。 我清除了列表
list.clear() 命令清除該列表中的所有對象。
但是,我的應用程序仍然消耗 190 MB 內存。
如何正確處理列表?
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
}
list.Clear() 不會重置已分配列表的容量。 即使在調用 Clear() 之后,容量仍然與以前相同,這意味着底層數組尚未銷毀。
在 Clear() 之后調用 TrimExcess() 將重置容量,這可能最終釋放 memory。
但是,一旦 object 沒有被任何其他數據結構引用,它應該默認符合垃圾回收條件。
使用GC.Collect();
清除列表后。
編輯:嘗試FileProps=null;
而是清除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.