4

我们在 MemoryMappedFile 中加载了一个 222MB 的文件来访问原始数据。使用 write 方法更新此数据。经过一些计算,数据应重置为文件的原始值。我们目前正在通过释放类并创建一个新实例来做到这一点。这在很多时候都很顺利,但有时 CreateViewAccessor 会崩溃,但会出现以下异常:

System.Exception:没有足够的存储空间来处理此命令。---> System.IO.IOException: 没有足够的存储空间来处理这个命令。

在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.MemoryMappedFiles.MemoryMappedView.CreateView(SafeMemoryMappedFileHandle > memMappedFileHandle, MemoryMappedFileAccess access, Int64 offset, Int64 size) 在 System.IO.MemoryMappedFiles.MemoryMappedFile.CreateViewAccessor( Int64 偏移,Int64 > 大小,MemoryMappedFileAccess 访问)

以下类用于访问内存映射文件:

public unsafe class MemoryMapAccessor : IDisposable
{
    private MemoryMappedViewAccessor _bmaccessor;
    private MemoryMappedFile _mmf;
    private byte* _ptr;
    private long _size;

    public MemoryMapAccessor(string path, string mapName)
    {
        FileInfo info = new FileInfo(path);
        _size = info.Length;

        using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
            _mmf = MemoryMappedFile.CreateFromFile(stream, mapName, _size, MemoryMappedFileAccess.Read, null, HandleInheritability.None, false);

        _bmaccessor = _mmf.CreateViewAccessor(0, 0, MemoryMappedFileAccess.CopyOnWrite);
        _bmaccessor.SafeMemoryMappedViewHandle.AcquirePointer(ref _ptr);
    }

    public void Dispose()
    {
        if (_bmaccessor != null)
        {
            _bmaccessor.SafeMemoryMappedViewHandle.ReleasePointer();
            _bmaccessor.Dispose();
        }
        if (_mmf != null)
            _mmf.Dispose();
    }


    public long Size { get { return _size; } }

    public byte ReadByte(long idx)
    {
        if ((idx >= 0) && (idx < _size))
        {
            return *(_ptr + idx);
        }

        Debug.Fail(string.Format("MemoryMapAccessor: Index out of range {0}", idx));
        return 0;
    }

    public void Write(long position, byte value)
    {
        if ((position >= 0) && (position < _size))
        {
            *(_ptr + position) = value;
        }
        else
            throw new Exception(string.Format("MemoryMapAccessor: Index out of range {0}", position));
    }
}

此问题的可能原因是什么,是否有任何解决方案/解决方法?

4

1 回答 1

5
  • 尝试使用 x64 平台处理而不是 x32平台

  • 确保每次都手动处理 MemoryMapAccessor。根据您的实现,GC不会为您调用 Dispose - 这里有很好的解释正确使用 IDisposable 接口

  • 调用 Dispose 不会使您的变量为空,因此 GC 会等到它理解没有人使用这些变量。确保您的变量在 Dispose 之后超出范围,或者只是将它们标记为空。最简单的情况是在 Dispose 中处理 - 如果不再需要变量,为什么不将它们标记为 null?这允许 GC 更快地吃掉它们。

  • 这是关于此类错误的另一个好主题(尽管提到了 VS.Net IDE,但它包含可能发生此类错误的详细信息)没有足够的存储空间可用于在 VisualStudio 2008 中处理此命令如果您经常需要非常大的部分的想法之一内存,这会导致内存碎片,所以当你仍然有足够的总可用内存时,你没有足够大的可用内存块。

  • 对于您的特定情况,将数组从文件中读入内存可能是一个很好的主意byte[],尽管不涉及非托管资源。通过一些幸运的编码,它可能会导致 CLR 更好地管理内存;但您需要谨慎对待此类决定。

于 2013-03-28T08:45:51.647 回答