C#读取记事本,里面有600万条数据,放入数组时:System.OutOfMemoryException
原因:使用文件流,然后读取文件内容,再解析的时候,会报内存溢出
处理办法:使用/n分隔
/// <summary> /// 通过记事本,获取CRM所有客户的某个字段 /// </summary> /// <returns></returns> public static List<string> GetFieldByText(string fieldname) { try { string filePath = string.Empty; string debugPath = System.Environment.CurrentDirectory;// Directory.GetCurrentDirectory(); filePath = debugPath + $"\\CrmData\\{fieldname}.txt"; int intNoOfLines = 0; using (StreamReader oReader = new StreamReader(filePath)) { while (oReader.ReadLine() != null) intNoOfLines++; } string[] strArrLines = new string[intNoOfLines]; int intIndex = 0; using (StreamReader oReader = new StreamReader(filePath)) { string strLine; while ((strLine = oReader.ReadLine()) != null) { strArrLines[intIndex++] = strLine; } } #region xxxxx下面的方式会超出内存xxxxxxxxxxxxxx //if (!File.Exists(filePath)) //{ // return new string[] { }; //} //StreamReader streamReader = new StreamReader(filePath, Encoding.GetEncoding("GB2312")); //string content = streamReader.ReadToEnd(); //streamReader.Close(); //string[] textList = content.Split('&'); //内存溢出 #endregion return strArrLines.ToList(); } catch (Exception ex) { LoggerHelper.Log("CDPDataHelperError", "报错:" + ex.Message); throw; } }
发现的源地址:https://www.codenong.com/13415916/
记得收藏并关注,掌握更多相关知识!!!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)