zoukankan      html  css  js  c++  java
  • 使用split进行大数据分割时内存溢出解决方案

    某个文本文件中存储了60W条记录,以\r\n作为分隔符,现在需要从文本中一次性取出所有值并存放到一个string[]数组中。

    StreamReader sr = new StreamReader(strFilePath, System.Text.UnicodeEncoding.GetEncoding("utf-8"));

    string strContent = sr.ReadToEnd();

    string[] strArr = strContent.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

    以上方式非常不建议使用,当数据量很大时很容易抛出内存溢出异常,这是由于string类型自身的安全性导致的,不建议使用string类型的对象临时保存大量的数据。

    我们应该采用下面的方式来进行大数据量的处理。

    代码
    List<string> List = new List<string>();
    using (StreamReader _StreamReaderKey = new StreamReader(strTermCacheFilePath + fileInfo.Name))
    {
    string strLine = "";
    while (!string.IsNullOrEmpty((strLine = _StreamReaderKey.ReadLine())))
    {
    List.Add(strLine);
    }
    }
  • 相关阅读:
    典型并发任务
    第九章使用共享变量实现并发
    第八章goroutine和通道
    第七章接口
    第六章方法
    第一章
    第四章复合数据类型
    第三章基础数据类型
    Django其他
    VUE学习日记(五) ---- 组件定义 component
  • 原文地址:https://www.cnblogs.com/zengen/p/1898901.html
Copyright © 2011-2022 走看看