Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我想将我的对象序列化为字符串,但我确实遇到了一个错误";System.OutOfMemoryException“;_C#_.net - Fatal编程技术网

C# 我想将我的对象序列化为字符串,但我确实遇到了一个错误";System.OutOfMemoryException“;

C# 我想将我的对象序列化为字符串,但我确实遇到了一个错误";System.OutOfMemoryException“;,c#,.net,C#,.net,我想将我的对象序列化为字符串,但出现了一个错误: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.Text.StringBuilder.ToString() at System.IO.StreamReader.ReadToEnd() at TEG.USBSharing.Utility.FileUtility.Val

我想将我的对象序列化为字符串,但出现了一个错误:

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
       at System.Text.StringBuilder.ToString()
       at System.IO.StreamReader.ReadToEnd()
   at TEG.USBSharing.Utility.FileUtility.ValidateReadByStreamReader(StreamReader streamReader)
这是我的代码: (1) 系列化

public static string Serialization<T>(T obj)
{
    using (MemoryStream memoryStream = new MemoryStream())
    using (StreamReader reader = new StreamReader(memoryStream))
    {
        DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
        serializer.WriteObject(memoryStream, obj);
        memoryStream.Position = 0;
        return FileUtility.ValidateReadByStreamReader(reader);
    }
}
(3) 我的目标

DataPacKet m_DataPacKet = new DataPacKet() {
    Command = DataPacKet.COMMAND_SEND_FILE_TRANSFER_CONTENT,
    GuidId = m_Guid,
    Data = new DataPacKetContent()
    {
        Sequence = i,
        DataBase64 = Convert.ToBase64String(m_SendingBuffer),
    },
    TransferType = dataPacketRequest.TransferType }; string m_MessageFileDataPacKet =
JsonUtility.Serialization<DataPacKet>(m_DataPacKet);
DataPacKet m_DataPacKet=新数据包(){
Command=DataPacKet.Command\u发送\u文件\u传输\u内容,
GuidId=m_Guid,
数据=新数据包内容()
{
序列=i,
DataBase64=Convert.tobase64字符串(m_SendingBuffer),
},
TransferType=dataPacketRequest.TransferType};字符串m_MessageFileDataPacKet=
序列化(m_数据包);

感谢您的帮助。

如果您确定系统有足够的内存容纳对象,您可以尝试编译x64:

如果您使用的是Visual Studio,则可以转到项目的属性,并在“生成”选项卡下将平台目标设置为x64


如果您确定您的系统有足够的内存来容纳对象,您可以尝试为x64编译:

如果您使用的是Visual Studio,则可以转到项目的属性,并在“生成”选项卡下将平台目标设置为x64


您试图序列化的对象是什么?您正在将整个流读取到内存中,显然太大而无法容纳。
ValidateReadByStreamReader
在做什么?这肯定不能证实任何事情。也许您可以设计代码,在读取流时对其进行处理,而不是一次全部处理。同意Rob的观点,这是关于内存消耗的问题对象中有什么?不要返回字符串。字符串是按值复制的。也就是说,每个字符串从一个方法传递到另一个方法会消耗内存。返回
MemorStream
StreamReader
。通过引用复制对象(仅32或64位)。进一步处理流中的数据。您试图序列化的对象是什么?您正在将整个流读取到内存中,而内存显然太大,无法容纳。
ValidateReadByStreamReader
在做什么?这肯定不能证实任何事情。也许您可以设计代码,在读取流时对其进行处理,而不是一次全部处理。同意Rob的观点,这是关于内存消耗的问题对象中有什么?不要返回字符串。字符串是按值复制的。也就是说,每个字符串从一个方法传递到另一个方法会消耗内存。返回
MemorStream
StreamReader
。通过引用复制对象(仅32或64位)。进一步处理流中的数据。
DataPacKet m_DataPacKet = new DataPacKet() {
    Command = DataPacKet.COMMAND_SEND_FILE_TRANSFER_CONTENT,
    GuidId = m_Guid,
    Data = new DataPacKetContent()
    {
        Sequence = i,
        DataBase64 = Convert.ToBase64String(m_SendingBuffer),
    },
    TransferType = dataPacketRequest.TransferType }; string m_MessageFileDataPacKet =
JsonUtility.Serialization<DataPacKet>(m_DataPacKet);