I have a .NET Core 2.0 that is processing a list of over 30,000 XML files and seems to have a memory leak.
Right now all I have the program doing is reading one file at a time, adding that files contents to a List then moving on to the next file.
Nothing is happening with these files, but my program memory is rising .5 MB per file. The files themselves are anything between 5KB - 10KB.
Here is the process method.
public async Task ProcessModelFile<TModel>(List<string> dataFileList) where TModel : InstitutionModel
{
foreach (var file in dataFileList)
{
var xmlLoadFile = new XmlLoadFile<TModel>();
xmlLoadFile.AddFile(file, rootElementName: $"ArrayOf{TextService.GetClassNameFromType<TModel>()}");
}
}
And the AddFile method and class:
internal class XmlLoadFile<TModel> where TModel : class
{
public List<TModel> ModelList { get; set; }
internal void AddFile(string file, string rootElementName = "", bool HasHistoricalCutOff = false, DateTime? HistoricalCutOffDate = null)
{
using (XmlReader reader = XmlReader.Create(file, new XmlReaderSettings { CheckCharacters = false }))
{
XmlSerializer serializer;
if (!string.IsNullOrWhiteSpace(rootElementName))
{
XmlRootAttribute rootElement = new XmlRootAttribute();
rootElement.ElementName = rootElementName;
serializer = new XmlSerializer(typeof(List<TModel>), rootElement);
}
else
{
serializer = new XmlSerializer(typeof(List<TModel>));
}
ModelList.AddRange((List<TModel>)serializer.Deserialize(reader));
}
}
}
Every time the foreach loop runs, my memory increases .5MB and with the amount of files I have it goes over 15GB quickly. Debugging in VS and taking snapshots of the Memory it does not account for the memory that is being used.
Garbage Collection does not seem to be releasing this memory properly.
> with a capacity equal to the number of files. Once processing is done, flatten the list eg with `SelectMany()`
– Panagiotis Kanavos Oct 04 '18 at 16:22