用c#XmlDocument加载多个xml文件导致内存泄漏 [英] Loading multiple xml files with c# XmlDocument leading to memory leakage

查看:27
本文介绍了用c#XmlDocument加载多个xml文件导致内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 XmlDocument 处理大量 xml 文件时遇到了严重的问题.这个想法是从大约 5000 个 .xml 文档(每个大约 20 MB)中提取特定数据,以文本格式保存,然后导入 MySQL DB.这个任务应该每天完成.

I am having serious troubles with processing a big number of xml files using XmlDocument. The idea is to extract from about 5000 .xml docs (approx. 20 MB each) a certain data, which is saved in text format and then imported into MySQL DB. This task is supposed to be done every day.

我的问题是处理完每个xml文件后,系统内存并没有释放它.所以,所有的文件都堆积如山,直到所有的 RAM 都被占用并且应用程序开始运行非常缓慢(一旦硬盘开始帮助系统内存).

My problem is that after processing of each xml file, the system memory is not releasing it. So, all the documents are pilling up, until all the RAM is occupied and the application starts to run very slowly (once the hard drive starts helping the system memory).

我使用的是已创建的源代码,因此无法更改为其他类,例如 XmlReader 等,因此我一直在使用 XmlDocument.

I am using already created source code, so it is not possible to change to other classes like XmlReader and so on, so I am stuck with XmlDocument.

加载xml的函数是这样调用的:

The function for xml loading is called like this:

foreach (string s in xmlFileNames)
            {
                i++;
                if (mytest.LoadXml(s))
                    mytest.loadToExchangeTables();
            }

函数如下所示:

public bool LoadXml(string fileName)
    {

        XmlDocument myXml = new XmlDocument();
        myXml.Load(fileName);
        .............
        //searching for needed data
        .............
    }

任何想法可能是什么问题?为什么垃圾收集没有完成?

Any ideas what might be the problem? And why garbage collection is not done?

在此先非常感谢您!

推荐答案

尝试用 //搜索所需数据 注释该部分并再次运行测试,可能是这样你就不会释放一些 IDisposable(使用 using 或直接),同样适用于 loadToExchangeTables().

Try to comment that part with // searching for needed data and run test once again, it may be so you don't free something IDisposable (with the use of using or directly), same goes for loadToExchangeTables().

这篇关于用c#XmlDocument加载多个xml文件导致内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆