简体   繁体   中英

Clearing the memory associated to a large List

I followed the advice in this SO Question . It did not work for me. Here is my situation and my code associated to it

I have a very large list, it has 2.04M items in it. I read it into memory to sort it, and then write it to a .csv file. I have 11 .csv files that I need to read, and subsequently sort. The first iteration gives me a memory usage of just over 1GB. I tried setting the list to null . I tried calling the List.Clear() I also tried the List.TrimExcess() . I have also waited for GC to do its thing. By hoping that it would know that there are no reads or writes going to that list.

Here is my code that I am using. Any advice is always greatly appreciated.

foreach (var zone in zones)
{

    var filePath = string.Format("outputs/zone-{0}.csv", zone);

    var lines = new List<string>();

    using (StreamReader reader = new StreamReader(filePath))
    {

        var headers = reader.ReadLine();

        while(! reader.EndOfStream)
        {
            var line = reader.ReadLine();

            lines.Add(line);
        }

        //sort the file, then rewrite the file into inputs

        lines = lines.OrderByDescending(l => l.ElementAt(0)).ThenByDescending(l => l.ElementAt(1)).ToList();

        using (StreamWriter writer = new StreamWriter(string.Format("inputs/zone-{0}-sorted.csv", zone)))
        {
            writer.WriteLine(headers);
            writer.Flush();

            foreach (var line in lines)
            {
                writer.WriteLine(line);
                writer.Flush();
            }
        }

        lines.Clear();
        lines.TrimExcess();

    }
}

Try putting the whole thing in a using :

using (var lines = new List<string>())
{ ... }

Although I'm not sure about the nested using s.

Instead, where you have lines.Clear; , add lines = null; . That should encourage the garbage collector.

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