小编exc*_*ror的帖子

为什么我不能在计算机中利用4GB的RAM来处理C#中少于2GB的信息?

场景:我需要以数学方式处理超过1.5GB的文本和csv文件.我尝试使用SQL Server Express,但加载信息,即使使用BULK导入也需要很长时间,理想情况下我需要将整个数据集放在内存中,以减少硬盘IO.

有超过120,000,000条记录,但即使我尝试将信息过滤到一列(内存中),我的C#控制台应用程序也消耗~3.5GB内存来处理仅125MB(实际读入700MB)的文本.

似乎GC没有收集对字符串和字符串数组的引用,即使在将所有引用设置为null并使用using关键字封装IDisposable之后也是如此.

我认为罪魁祸首是String.Split()方法,它为每个逗号分隔值创建一个新字符串.

您可能会建议我甚至不应该将不需要的*列读入字符串数组中,但是忽略了这一点:如何将整个数据集放在内存中,以便我可以在C#中并行处理它?

我可以使用复杂的调度算法优化统计算法和协调任务,但这是我在遇到内存问题之前希望做的事情,而不是因为.

我已经包含了一个模拟我的环境的完整控制台应用程序,应该可以帮助复制问题.

任何帮助表示赞赏.提前致谢.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace InMemProcessingLeak
{
    class Program
    {
        static void Main(string[] args)
        {
            //Setup Test Environment. Uncomment Once
            //15000-20000 files would be more realistic
            //InMemoryProcessingLeak.GenerateTestDirectoryFilesAndColumns(3000, 3);
            //GC
            GC.Collect();
            //Demostrate Large Object Memory Allocation Problem (LOMAP)
            InMemoryProcessingLeak.SelectColumnFromAllFiles(3000, 2);
        }
    }

    class InMemoryProcessingLeak
    {
        public static List<string> SelectColumnFromAllFiles(int filesToSelect, int column)
        {
            List<string> allItems = new List<string>();
            int fileCount = filesToSelect;
            long fileSize, totalReadSize …
Run Code Online (Sandbox Code Playgroud)

c# memory string performance garbage-collection

6
推荐指数
1
解决办法
1472
查看次数

标签 统计

c# ×1

garbage-collection ×1

memory ×1

performance ×1

string ×1