【问题标题】:System Out of memory exception when deserialising a byte array反序列化字节数组时系统内存不足异常
【发布时间】:2017-02-17 13:36:59
【问题描述】:

我有一个带有 SQL 原始输出的表,我正在使用 SQL 命令阅读器循环遍历一个表并反序列化数据。反序列化的方法是抛出 System.OutOfMemory 异常。

我正在使用 .Net 4.5 和 SQL Server 2014。

using (SqlDataReader rdr = cmd.ExecuteReader())
{
    while (rdr.Read())
    {  
       byte[] ioNames = null;

        long ioNamesArraySize = rdr.GetBytes(ApplicationConstants.ord_IONames, 0, null, 0, 0);
        ioNames = new byte[ioNamesArraySize];
        ioNames = (byte[])rdr[ApplicationConstants.ord_IONames];

    // Get the data.
    byte[] outputs = null;
    long outputsArraySize = rdr.GetBytes(ApplicationConstants.ord_RawData, 0, null, 0, 0);
    outputs = new byte[outputsArraySize];
    outputs = (byte[])rdr[ApplicationConstants.ord_RawData];

    // Extract the InstrumentOutputNames and Outputs.
    object[][] data = null;
    data = outputs.DeserializeFromByteArray<object[][]>();
    // The below line throws the exception.                
    var iPms = ioNames.DeserializeFromByteArray<IEnumerable<InstrumentParameter>>();
    iPms = iPms.ToList();
}

/// <summary>
/// Deserializes the specified byte array.
/// </summary>
/// <typeparam name="T">Type to Use</typeparam>
/// <param name="byteArray">The byte array.</param>
/// <returns>T.</returns>
public static T DeserializeFromByteArray<T>(this byte[] byteArray)
{
    using (var ms = new MemoryStream(byteArray))
    {
        return (T)new BinaryFormatter().Deserialize(ms);
    }
}

【问题讨论】:

  • 什么是ioNames?您的意思是改为调用 outputs.DeserializeFromByteArray 吗?
  • @Polyfun - 感谢您对此进行调查。我的错,我现在已经更新了代码。如有任何问题,请随时提出。
  • @SuperOil - 谢谢,您的编辑将使问题更具可读性,我对 StackOverflow 还很陌生,仍在学习。
  • 请确保我正确格式化了您的代码。 ionames 似乎在 while 循环内声明,但在外部使用。
  • @SuperOli 再次感谢您。这是一个复制粘贴错误。

标签: c# arrays sql-server memory byte


【解决方案1】:

首先感谢大家对此进行调查。只需更改应用程序构建属性即可解决此问题。

  • 取消选中首选 32 位
  • 选择 64 位而不是任何 CPU。

这很好用。希望有人会觉得这很有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多