使用内存映射文件来补偿C#中的内存不足

时间:2012-03-28 07:48:07

标签: c# out-of-memory

我有一个问题 - 以前我用的是算法来解决其中的一部分(参见 Combine LINQ queries ),而且我遇到了一个很大的问题。

在大约540k的目录中,它因内存不足而崩溃。 :(

我正在尝试处理和存储公司的SAN文件信息,我们需要这样做,因为我们有人将数据保存了25年并且他们不需要,但很难跟踪。总共最多70 TB的文件。所以,你可以想象,它是很多文件。

从我读过的内容来看,内存映射文件不能动态吗?这是真的?我以前无法确定有多少文件+目录。

如果没有,(请说不是),有人可以给我一个关于如何制作动态映射文件的简短示例( Combine LINQ queries 问题中提供的代码)。简而言之,我在内存中创建了一个目录结构,其中包含目录→目录+文件(名称,大小,访问日期,修改日期和创建日期)。

任何线索都会受到赞赏,因为如果可能的话,这会解决我的问题。

1 个答案:

答案 0 :(得分:2)

如果你无法将整个内容整合到内存中,你可以用IEnumerable流式传输你的数据。下面是一个例子。我一直在玩MemoryMapped文件,因为我需要最后一滴perf,但到目前为止我一直坚持使用BinaryReader / Writer。

对于DB倡导者:当你真的需要最后一滴perf时,我也会自己做二进制文件。退出进程到数据库确实增加了开销。此外,整个安全/日志记录,ACID等确实加起来。

这是一个流式传输f_results类的示例。

修改

更新示例以显示如何写/读目录信息树。我保留了一个包含所有目录的文件。此树一次加载到内存中,然后指向所有f_results所在的文件。 您仍然需要为每个目录创建一个单独的文件,其中包含所有文件的f_results。如何做到这一点取决于你的代码,但你应该能够弄明白。

祝你好运!

public class f_results {
    public String name { get; set; }
    public DateTime cdate { get; set; }
    public DateTime mdate { get; set; }
    public DateTime adate { get; set; }
    public Int64 size { get; set; }

    // write one to a file
    public void WriteTo(BinaryWriter wrtr) {
        wrtr.Write(name);
        wrtr.Write(cdate.Ticks);
        wrtr.Write(mdate.Ticks);
        wrtr.Write(adate.Ticks);
        wrtr.Write(size);
    }

    // read one from a file
    public f_results(BinaryReader rdr) {
        name = rdr.ReadString();
        cdate = new DateTime(rdr.ReadInt64());
        mdate = new DateTime(rdr.ReadInt64());
        adate = new DateTime(rdr.ReadInt64());
        size = rdr.ReadInt64();
    }

    // stream a whole file as an IEnumerable (so very little memory needed)
    public static IEnumerable<f_results> FromFile(string dataFilePath) {
        var file = new FileStream(dataFilePath, FileMode.Open);
        var rdr = new BinaryReader(file);
        var eos = rdr.BaseStream.Length;
        while (rdr.BaseStream.Position < eos) yield return new f_results(rdr);
        rdr.Close();
        file.Close();
    }
}

class Program {
    static void Main(string[] args) {

        var d1 = new DirTree(@"C:\",
            new DirTree(@"C:\Dir1",
                new DirTree(@"C:\Dir1\Dir2"),
                new DirTree(@"C:\Dir1\Dir3")
                ),
                new DirTree(@"C:\Dir4",
                new DirTree(@"C:\Dir4\Dir5"),
                new DirTree(@"C:\Dir4\Dir6")
                ));

        var path = @"D:\Dirs.dir";

        // write the directory tree to a file
        var file = new FileStream(path, FileMode.CreateNew | FileMode.Truncate);
        var w = new BinaryWriter(file);
        d1.WriteTo(w);
        w.Close();
        file.Close();

        // read it from the file
        var file2 = new FileStream(path, FileMode.Open);
        var rdr = new BinaryReader(file2);
        var d2 = new DirTree(rdr);

        // now inspect d2 in debugger to see that it was read back into memory

        // find files bigger than (roughly) 1GB
        var BigFiles = from f in f_results.FromFile(@"C:\SomeFile.dat")
                       where f.size > 1e9
                       select f;
    }
}

class DirTree {
    public string Path { get; private set; }
    private string FilesFile { get { return Path.Replace(':', '_').Replace('\\', '_') + ".dat"; } }

    public IEnumerable<f_results> Files() {
        return f_results.FromFile(this.FilesFile);
    }

    // you'll want to encapsulate this in real code but I didn't for brevity
    public DirTree[] _SubDirectories;

    public DirTree(BinaryReader rdr) {
        Path = rdr.ReadString();
        int count = rdr.ReadInt32();
        _SubDirectories = new DirTree[count];
        for (int i = 0; i < count; i++) _SubDirectories[i] = new DirTree(rdr);
    }

    public DirTree( string Path, params DirTree[] subDirs){
        this.Path = Path;
        _SubDirectories = subDirs;
    }

    public void WriteTo(BinaryWriter w) {
        w.Write(Path);           
        w.Write(_SubDirectories.Length);
        // depth first is the easiest way to do this
        foreach (var f in _SubDirectories) f.WriteTo(w);
    }
}

}