MemoryMappedFile.CreateViewStream()的托管内存使用

本文关键字:内存 CreateViewStream MemoryMappedFile | 更新日期: 2023-09-27 18:19:16

MemoryMappedFile.CreateViewStream(0, len)将分配一个大小为len的托管内存块,或者它将分配较小的缓冲区,作为非托管数据的滑动窗口?

我想知道,因为我的目标是取代一个中间缓冲区的反序列化,这是一个MemoryStream今天,这给我的麻烦大数据集,因为缓冲区的大小和因为LOH碎片。

如果viewstream的内部缓冲区变成了相同的大小,那么这个切换就没有意义了。

编辑:

在一个快速测试中,我在比较MemoryStream和memorymap文件时发现了这些数字。GC.GetTotalMemory(true)/1024Process.GetCurrentProcess.VirtualMemorySize64/1024读数

分配1GB内存流:

                     Managed           Virtual 
Initial:               81 kB        190 896 kB
After alloc:    1 024 084 kB      1 244 852 kB

如预期的那样,有1g的托管内存和虚拟内存。现在,对于MemoryMappedFile:

                     Managed           Virtual 
Initial:               81 kB        189 616 kB    
MMF allocated:         84 kB        189 684 kB
1GB viewstream allocd: 84 kB      1 213 368 kB
Viewstream disposed:   84 kB        190 964 kB

所以使用一个不太科学的测试,我的假设是ViewStream只使用非托管数据。正确吗?

MemoryMappedFile.CreateViewStream()的托管内存使用

这样的MMF并不能解决您的问题。程序在OOM上爆炸是因为虚拟内存空间中没有足够大的洞来容纳分配。可以看出,您仍然在使用MMF消耗VM地址空间。

使用一个小的滑动视图将是一个解决方案,但这与写入文件没有任何不同。这就是MMF在重新映射视图时所做的事情,它需要将脏页刷新到磁盘。简单地流式传输到FileStream是正确的解决方法。这仍然使用RAM,文件系统缓存有助于提高写入速度。如果您有1g的可用RAM(这在现在并不难获得),那么写入FileStream只是一个内存到内存的复制。非常快,每秒5gb以上。文件在后台以惰性方式写入。

在Windows中,过于努力地将数据保存在内存中是没有效率的。内存中的私有数据由分页文件提供支持,当Windows需要内存用于其他进程时,这些数据将被写入该文件。当你再次访问它时,再读一遍。这很慢,你用的内存越多,情况就越糟。与任何按需分页的虚拟内存操作系统一样,磁盘和内存之间的区别很小。

给出http://msdn.microsoft.com/en-us/library/system.io.memorymappedfiles.memorymappedfile.aspx上的例子,在我看来,你得到一个滑动窗口,至少这是我在阅读示例时的解释。

为方便起见,这里有一个示例:

    using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
class Program
{
    static void Main(string[] args)
    {
        long offset = 0x10000000; // 256 megabytes 
        long length = 0x20000000; // 512 megabytes 
        // Create the memory-mapped file. 
        using (var mmf = MemoryMappedFile.CreateFromFile(@"c:'ExtremelyLargeImage.data", FileMode.Open,"ImgA"))
        {
            // Create a random access view, from the 256th megabyte (the offset) 
            // to the 768th megabyte (the offset plus length). 
            using (var accessor = mmf.CreateViewAccessor(offset, length))
            {
                int colorSize = Marshal.SizeOf(typeof(MyColor));
                MyColor color;
                // Make changes to the view. 
                for (long i = 0; i < length; i += colorSize)
                {
                    accessor.Read(i, out color);
                    color.Brighten(10);
                    accessor.Write(i, ref color);
                }
            }
        }
    }
}
public struct MyColor
{
    public short Red;
    public short Green;
    public short Blue;
    public short Alpha;
    // Make the view brigher. 
    public void Brighten(short value)
    {
        Red = (short)Math.Min(short.MaxValue, (int)Red + value);
        Green = (short)Math.Min(short.MaxValue, (int)Green + value);
        Blue = (short)Math.Min(short.MaxValue, (int)Blue + value);
        Alpha = (short)Math.Min(short.MaxValue, (int)Alpha + value);
    }
}