开发者

"Out of Memory" Exception when Decrypting an XML file

开发者 https://www.devze.com 2023-01-02 20:17 出处:网络
I\'m using the System.Security.Cryptography library to encrypt and decrypt xml files. Recently though I\'ve been getting OOM (Out of Memory) exceptions while trying to decrypt a 75MB file. Here\'s the

I'm using the System.Security.Cryptography library to encrypt and decrypt xml files. Recently though I've been getting OOM (Out of Memory) exceptions while trying to decrypt a 75MB file. Here's the code I'm using:

    using System.Security.Cryptography.Xml;
    ...
    public static XmlDocument DecryptIntoXmlDoc(string filename)
    {
        //Decrypt the XML
        XmlDocument xmldoc = new XmlDocument();
        EncryptedXml exml = new EncryptedXml(xmldoc);
        TripleDESCryptoServiceProvider ekey = new TripleDESCryptoServiceProvider();
        ASCIIEncoding encoding = new ASCIIEncoding();

        ekey.Key = encoding.GetBytes(GetMasterKey());
        exml.AddKeyNameMapping("ekey", ekey);
        xmldoc.Load(filename);

        // -- THROWS THE OOM ERROR --
        exml.DecryptDocument();

        //Clear exml
        exml = null;

        return xmldoc;
    }

As soon as .DecryptDocument() is called I get the following error开发者_JAVA技巧:

 Exception of type 'System.OutOfMemoryException' was thrown.

I haven't been able to find anyone else having this problem, but I have read that if the xml tags aren't properly named/nested, the file can be very large when loaded into memory. Would renaming my XML tags to shorter names reduce the size? Is there a way to nest xml tags to reduce the file size?

Is there anything else I could do?


  • Run your program under the debugger
  • When the OOM exception is thrown, pause and examine the object graph rooted at exml. What's actually occupying most of the memory?

Immutable strings are probably the killer here, since some xml parsers can wind up holding your entire doc in memory multiple times.

0

精彩评论

暂无评论...
验证码 换一张
取 消