I'm having issues releasing the memory Saxon is using for loading the file into memory. This is a reduced version of the code we have:
Task newTask = Task.Run(() =>
{
Processor processor = new Processor();
DocumentBuilder documentBuilder = processor.NewDocumentBuilder();
documentBuilder.IsLineNumbering = true;
documentBuilder.WhitespacePolicy = WhitespacePolicy.PreserveAll;
XQueryCompiler compiler = processor.NewXQueryCompiler();
string query = BuildXqueryString();
if (!String.IsNullOrEmpty(query))
{
XQueryExecutable executable = compiler.Compile(query);
XQueryEvaluator evaluator = executable.Load();
evaluator.ContextItem = documentBuilder.Build(xmlNode);
var evaluations = evaluator.Evaluate();
}
}
We are running the code inside a Task
because we are having lots of evaluations happening at the same time, many of them with XML files up to 2GB in size. For this reason we need the memory used by Saxon during the execution of the Build()
method released as soon as possible, but what is happening is that the memory is not completely reclaimed by the garbage collector. After each evaluation the memory usage rises and rises, and is never completely freed, so with a couple of these big evaluations the memory usage rises to its max. I've tried calling the Dispose()
method of Task
, setting all the variables to null, but nothing seems to work. Any ideas what could be happening?
Edit:
Thank you all for your answers. I realized that the issue was actually not in the Saxon code, but instead with a XmlDocument that wasn't releasing the memory. Sorry for the confusion.