简体   繁体   中英

Memory not releasing .Net Application

I had a list of objects. The count of objects in the list is approx 5,06,011.

And it consuming 190MB in ram.

After some time I don't need that list. I cleared the list

list.clear() command clears all objects from that list.

But still, my application consuming 190 MB in ram.

How to dispose the list properly?

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() will not reset the capacity of the list that has already been allocated. Even after calling Clear(), the capacity remains same as before, meaning that the underlying array is not yet destroyed.

Calling TrimExcess() after Clear() will reset the capacity which might eventually free the memory.

But, once the object is not referenced by any other data structure, it should be eligible for garbage collection by default.

use GC.Collect(); after clearing the list.

edit: try FileProps=null; instead clearing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM