简体   繁体   English

Saxon内存未释放

[英]Saxon memory not being released

I'm having issues releasing the memory Saxon is using for loading the file into memory. 我在释放Saxon用于将文件加载到内存中使用的内存时遇到问题。 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. 我们正在Task中运行代码,因为我们同时进行了许多评估,其中许多评估使用的XML文件最大为2GB。 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. 因此,我们需要尽快执行Build()方法时由Saxon使用的内存,但实际上是垃圾回收器没有完全回收该内存。 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. 我尝试调用TaskDispose()方法,将所有变量设置为null,但似乎没有任何效果。 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. 我意识到这个问题实际上不在撒克逊人的代码中,而是与不释放内存的XmlDocument有关。 Sorry for the confusion. 对困惑感到抱歉。

Check if any of the classes of which instances are used inside the task are disposable, they could be holding resources until dispose is called explicitly (after being done with their job). 检查在任务中使用的实例的任何类是否是可丢弃的,它们是否可以保留资源,直到显式调用dispose(在完成其工作之后)。 also checkout using blocks. using块结帐。

Also noted from the official documentation : 还从官方文档中指出:

The first thing the application needs to do is to create a Processor. 应用程序需要做的第一件事是创建一个处理器。 The Processor holds configuration information for Saxon, and shared resources such as the name pool and schema pool. 处理器保存Saxon的配置信息以及共享资源,例如名称池和架构池。 It is possible to run multiple processors concurrently if required, but it is usually more economical for all Saxon processes within a single application to use the same Processor. 如果需要,可以同时运行多个处理器,但是通常单个应用程序中的所有Saxon进程使用同一处理器更为经济。

so how about using the same processor instance for all your tasks ? 那么如何对所有任务使用相同的处理器实例呢? it could help in case it uses cached resources. 如果使用缓存的资源,它可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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