c#反序列化为文件中的对象 - 内存不足

时间:2016-10-17 23:17:30

标签: c# serialization out-of-memory

我发布并回答了一个关于如何将一个相当大的(~150兆)对象序列化到文件而不会出现内存不足异常的问题。这现在有效。现在我需要将文件反序列化为一个对象。

我在此代码的缓冲区分配部分出现内存不足错误。我认为有一种方法可以安排流,所以我不必先读入临时缓冲区,但需要帮助。

public static object Deserialize(Type objType, FileInfo xmlDocFile)
{
    object returnValue = null;
    if (xmlDocFile != null && objType != null && xmlDocFile.Exists)
    {
        DataContractSerializer formatter = new DataContractSerializer(objType);
        ASCIIEncoding encoder = new ASCIIEncoding();
        byte[] buffer = null;
        using (FileStream textFile = new FileStream(xmlDocFile.FullName, FileMode.Open))
        {
            buffer = new byte[textFile.Length];   // Out-of-memory thrown here
            textFile.Read(buffer, 0, buffer.Length);
        }
        XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(buffer, new XmlDictionaryReaderQuotas());
        returnValue = formatter.ReadObject(reader, true);
    }
    return returnValue;
}

我用来序列化对象的代码就是这个

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();
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

无需将文件读入内存。将流传递到DataContractSerializer

public static object Deserialize(Type objType, FileInfo xmlDocFile)
{
    object returnValue = null;
    if (xmlDocFile != null && objType != null && xmlDocFile.Exists)
    {
        DataContractSerializer formatter = new DataContractSerializer(objType);

        using (FileStream textFile = new FileStream(xmlDocFile.FullName, FileMode.Open))
        {
            returnValue = formatter.ReadObject(textFile);
        }
    }
    return returnValue;
}

答案 1 :(得分:0)

我提出的解决方案(与Petrov非常接近)是:

public static object Deserialize(Type objType, FileInfo xmlDocFile)
{
    object returnValue = null;            
    if (xmlDocFile != null && objType != null && xmlDocFile.Exists)
    {
        DataContractSerializer formatter = new DataContractSerializer(objType);
        using (FileStream fs = File.Open(xmlDocFile.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            using (BufferedStream bs = new BufferedStream(fs))
            {
                XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(bs, Encoding.UTF8, new XmlDictionaryReaderQuotas(), null);
                returnValue = formatter.ReadObject(reader, true);
            }
        }
    }

    return returnValue;
}