简体   繁体   English

C#序列化xmlwriter字符串编写器内存不足,无法使用大型对象

[英]c# serialization xmlwriter stringwriter out of memory for large objecthelp

I am managing a large project and need to serialize and send an object in xml format. 我正在管理一个大型项目,需要序列化并以xml格式发送对象。 The object is ~130 mb. 对象为〜130 mb。

(NOTE: I did not write this project, so making edits outside of this method, or drastically changing the architecture is not an option. It works great normally, but when the object is this large, it throws out of memory exception. I need to do it another way to handle large objects.) (注意:我没有写这个项目,所以在此方法之外进行编辑或彻底更改体系结构是不可行的。它通常可以正常工作,但是当对象太大时,它将抛出内存不足异常。我需要以另一种方式处理大型物体。)

The current code is this: 当前代码是这样的:

public static string Serialize(object obj)
{
    string returnValue = null;

    if (null != obj)
    {
        System.Runtime.Serialization.DataContractSerializer formatter = new System.Runtime.Serialization.DataContractSerializer(obj.GetType());

        XDocument document = new XDocument();

        System.IO.StringWriter writer = new System.IO.StringWriter();
        System.Xml.XmlTextWriter xmlWriter = new XmlTextWriter(writer);

        formatter.WriteObject(xmlWriter, obj);
        xmlWriter.Close();

        returnValue = writer.ToString();
    }

    return returnValue;
}

It is throwing an out of memory exception right at returnValue = writer.ToString(). 它在returnValue = writer.ToString()处抛出了内存不足异常。

I rewrote it to use "using" blocks which I prefer: 我将其重写为使用我更喜欢的“使用”块:

public static string Serialize(object obj)
    {
        string returnValue = null;
        if (null != obj)
        {
            System.Runtime.Serialization.DataContractSerializer formatter = new System.Runtime.Serialization.DataContractSerializer(obj.GetType());
            using (System.IO.StringWriter writer = new System.IO.StringWriter())
            {
                using (System.Xml.XmlTextWriter xmlWriter = new XmlTextWriter(writer))
                {
                    formatter.WriteObject(xmlWriter, obj);
                    returnValue = writer.ToString();
                }
            }
        }
        return returnValue;
    }

researching this, it appears the ToString method on StringWriter actually uses double the RAM. 对此进行研究,看来StringWriter上的ToString方法实际上使用了两倍的RAM。 (I actually have plenty of RAM free, over 4 gb, so not really sure why I am getting an out of memory error). (实际上,我有大量可用的RAM,超过4 GB,因此不确定我为什么会出现内存不足错误)。

Well, I found the best solution was to serialize to a file directly, then instead of passing a string along, I pass the file: 好吧,我发现最好的解决方案是直接序列化到文件,然后传递文件而不是传递字符串:

public static void Serialize(object obj, FileInfo destination)
{
    if (null != obj)
    {
        using (TextWriter writer = new StreamWriter(destination.FullName, false))
        {
            XmlTextWriter xmlWriter = null;
            try
            {
                xmlWriter = new XmlTextWriter(writer);
                DataContractSerializer formatter = new DataContractSerializer(obj.GetType());
                formatter.WriteObject(xmlWriter, obj);
            }
            finally
            {
                if (xmlWriter != null)
                {
                    xmlWriter.Flush();
                    xmlWriter.Close();
                }
            }
        }
    }
}

Of course, now I have another problem which I will post ... and that is deserializing the file! 当然,现在我还有另一个问题要发布...,那就是反序列化文件!

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

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