• 【微软技术栈】C#.NET 内存映射文件


    本文内容

    1. 进程、视图和管理内存
    2. 使用内存映射文件编程
    3. 示例

    内存映射文件包含虚拟内存中文件的内容。 借助文件和内存空间之间的这种映射,应用(包括多个进程)可以直接对内存执行读取和写入操作,从而修改文件。 可以使用托管代码访问内存映射文件,就像本机 Windows 函数访问内存映射文件(如管理内存映射文件中所述)一样。

    内存映射文件分为两种类型:

    • 持久化内存映射文件

      持久化文件是与磁盘上的源文件相关联的内存映射文件。 当最后一个进程处理完文件时,数据保存到磁盘上的源文件中。 此类内存映射文件适用于处理非常大的源文件。

    • 非持久化内存映射文件

      非持久化文件是不与磁盘上的文件相关联的内存映射文件。 当最后一个进程处理完文件时,数据会丢失,且文件被垃圾回收器回收。 此类文件适合创建共享内存,以进行进程内通信 (IPC)。

    1、进程、视图和管理内存

    可以跨多个进程共享内存映射文件。 进程可以映射到相同的内存映射文件,只需使用文件创建进程分配的通用名称即可。

    必须创建整个或部分内存映射文件的视图,才能使用内存映射文件。 还可以为内存映射文件的同一部分创建多个视图,从而创建并发内存。 若要让两个视图一直处于并发状态,必须通过同一个内存映射文件创建它们。

    如果文件大于可用于内存映射的应用程序逻辑内存空间(在 32 位计算机中为 2 GB),可能也有必要使用多个视图。

    视图分为以下两种类型:流访问视图和随机访问视图。 使用流访问视图,可以顺序访问文件;建议对非持久化文件和 IPC 使用这种类型。 随机访问视图是处理持久化文件的首选类型。

    由于内存映射文件是通过操作系统的内存管理程序进行访问,因此文件会被自动分区到很多页面,并根据需要进行访问。 无需自行处理内存管理。

    下图展示了多个进程如何同时对同一个内存映射文件有多个重叠视图。

    下图显示了内存映射文件的多个重叠视图:

    显示内存映射文件的视图的屏幕截图。

    2、使用内存映射文件编程

    下表列出了与使用内存映射文件对象及其成员相关的指南。

    任务要使用的方法或属性
    从磁盘上的文件获取表示持久化内存映射文件的 MemoryMappedFile 对象。MemoryMappedFile.CreateFromFile 方法。
    获取表示非持久化内存映射文件的 MemoryMappedFile 对象(未与磁盘上的文件关联)。MemoryMappedFile.CreateNew 方法。

    - 或 -

    MemoryMappedFile.CreateOrOpen 方法。
    获取现有内存映射文件(持久化或非持久化)的 MemoryMappedFile 对象。MemoryMappedFile.OpenExisting 方法。
    获取内存映射文件的顺序访问视图的 UnmanagedMemoryStream 对象。MemoryMappedFile.CreateViewStream 方法。
    获取内存映射文件的随机访问视图的 UnmanagedMemoryAccessor 对象。MemoryMappedFile.CreateViewAccessor 方法。
    获取要与非托管代码结合使用的 SafeMemoryMappedViewHandle 对象。MemoryMappedFile.SafeMemoryMappedFileHandle 属性。

    - 或 -

    MemoryMappedViewAccessor.SafeMemoryMappedViewHandle 属性。

    - 或 -

    MemoryMappedViewStream.SafeMemoryMappedViewHandle 属性。
    将内存分配一直延迟到视图创建完成(仅限非持久化文件)。

    (若要确定当前系统页面大小,请使用 Environment.SystemPageSize 属性。)
    值为 MemoryMappedFileOptions.DelayAllocatePages 的 CreateNew 方法。

    - 或 -

    将 MemoryMappedFileOptions 枚举用作参数的 CreateOrOpen 方法。

    2.1 安全性

    可以在创建内存映射文件时应用访问权限,具体操作是运行以下需要将 MemoryMappedFileAccess 枚举用作参数的方法:

    若要指定打开现有内存映射文件所需的访问权限,可以运行需要将 MemoryMappedFileRights 用作参数的 OpenExisting 方法。

    另外,还可以添加包含预定义访问规则的 MemoryMappedFileSecurity 对象。

    若要将新的或更改后的访问规则应用于内存映射文件,请使用 SetAccessControl 方法。 若要从现有文件检索访问或审核规则,请使用 GetAccessControl 方法。

    3、示例

    3.1 持久化内存映射文件

    CreateFromFile 方法通过磁盘上的现有文件创建内存映射文件。

    下面的示例为极大文件的一部分创建内存映射视图,并控制其中一部分。

    1. using System;
    2. using System.IO;
    3. using System.IO.MemoryMappedFiles;
    4. using System.Runtime.InteropServices;
    5. class Program
    6. {
    7. static void Main(string[] args)
    8. {
    9. long offset = 0x10000000; // 256 megabytes
    10. long length = 0x20000000; // 512 megabytes
    11. // Create the memory-mapped file.
    12. using (var mmf = MemoryMappedFile.CreateFromFile(@"c:\ExtremelyLargeImage.data", FileMode.Open,"ImgA"))
    13. {
    14. // Create a random access view, from the 256th megabyte (the offset)
    15. // to the 768th megabyte (the offset plus length).
    16. using (var accessor = mmf.CreateViewAccessor(offset, length))
    17. {
    18. int colorSize = Marshal.SizeOf(typeof(MyColor));
    19. MyColor color;
    20. // Make changes to the view.
    21. for (long i = 0; i < length; i += colorSize)
    22. {
    23. accessor.Read(i, out color);
    24. color.Brighten(10);
    25. accessor.Write(i, ref color);
    26. }
    27. }
    28. }
    29. }
    30. }
    31. public struct MyColor
    32. {
    33. public short Red;
    34. public short Green;
    35. public short Blue;
    36. public short Alpha;
    37. // Make the view brighter.
    38. public void Brighten(short value)
    39. {
    40. Red = (short)Math.Min(short.MaxValue, (int)Red + value);
    41. Green = (short)Math.Min(short.MaxValue, (int)Green + value);
    42. Blue = (short)Math.Min(short.MaxValue, (int)Blue + value);
    43. Alpha = (short)Math.Min(short.MaxValue, (int)Alpha + value);
    44. }
    45. }

    下面的示例为另一个进程打开相同的内存映射文件。

    1. using System;
    2. using System.IO.MemoryMappedFiles;
    3. using System.Runtime.InteropServices;
    4. class Program
    5. {
    6. static void Main(string[] args)
    7. {
    8. // Assumes another process has created the memory-mapped file.
    9. using (var mmf = MemoryMappedFile.OpenExisting("ImgA"))
    10. {
    11. using (var accessor = mmf.CreateViewAccessor(4000000, 2000000))
    12. {
    13. int colorSize = Marshal.SizeOf(typeof(MyColor));
    14. MyColor color;
    15. // Make changes to the view.
    16. for (long i = 0; i < 1500000; i += colorSize)
    17. {
    18. accessor.Read(i, out color);
    19. color.Brighten(20);
    20. accessor.Write(i, ref color);
    21. }
    22. }
    23. }
    24. }
    25. }
    26. public struct MyColor
    27. {
    28. public short Red;
    29. public short Green;
    30. public short Blue;
    31. public short Alpha;
    32. // Make the view brigher.
    33. public void Brighten(short value)
    34. {
    35. Red = (short)Math.Min(short.MaxValue, (int)Red + value);
    36. Green = (short)Math.Min(short.MaxValue, (int)Green + value);
    37. Blue = (short)Math.Min(short.MaxValue, (int)Blue + value);
    38. Alpha = (short)Math.Min(short.MaxValue, (int)Alpha + value);
    39. }
    40. }

    3.2 非持久化内存映射文件

    CreateNew 和 CreateOrOpen 方法创建未映射到磁盘上现有文件的内存映射文件。

    下面的示例包含三个独立进程(控制台应用),以将布尔值写入内存映射文件。 各操作按下面的顺序发生:

    1. Process A 创建内存映射文件,并向其中写入值。

    2. Process B 打开内存映射文件,并向其中写入值。

    3. Process C 打开内存映射文件,并向其中写入值。

    4. Process A 读取并显示内存映射文件中的值。

    5. 在 Process A 处理完内存映射文件后,此文件立即被垃圾回收器回收。

    若要运行此示例,请按照以下步骤操作:

    1. 编译应用并打开三个命令提示符窗口。

    2. 在第一个命令提示符窗口中,运行 Process A

    3. 在第二个命令提示符窗口中,运行 Process B

    4. 返回到 Process A,再按 Enter。

    5. 在第三个命令提示符窗口中,运行 Process C

    6. 返回到 Process A,再按 Enter。

    Process A 的输出如下所示:

    1. Start Process B and press ENTER to continue.
    2. Start Process C and press ENTER to continue.
    3. Process A says: True
    4. Process B says: False
    5. Process C says: True

    Process A

    1. using System;
    2. using System.IO;
    3. using System.IO.MemoryMappedFiles;
    4. using System.Threading;
    5. class Program
    6. {
    7. // Process A:
    8. static void Main(string[] args)
    9. {
    10. using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("testmap", 10000))
    11. {
    12. bool mutexCreated;
    13. Mutex mutex = new Mutex(true, "testmapmutex", out mutexCreated);
    14. using (MemoryMappedViewStream stream = mmf.CreateViewStream())
    15. {
    16. BinaryWriter writer = new BinaryWriter(stream);
    17. writer.Write(1);
    18. }
    19. mutex.ReleaseMutex();
    20. Console.WriteLine("Start Process B and press ENTER to continue.");
    21. Console.ReadLine();
    22. Console.WriteLine("Start Process C and press ENTER to continue.");
    23. Console.ReadLine();
    24. mutex.WaitOne();
    25. using (MemoryMappedViewStream stream = mmf.CreateViewStream())
    26. {
    27. BinaryReader reader = new BinaryReader(stream);
    28. Console.WriteLine("Process A says: {0}", reader.ReadBoolean());
    29. Console.WriteLine("Process B says: {0}", reader.ReadBoolean());
    30. Console.WriteLine("Process C says: {0}", reader.ReadBoolean());
    31. }
    32. mutex.ReleaseMutex();
    33. }
    34. }
    35. }

    Process B

    1. using System;
    2. using System.IO;
    3. using System.IO.MemoryMappedFiles;
    4. using System.Threading;
    5. class Program
    6. {
    7. // Process B:
    8. static void Main(string[] args)
    9. {
    10. try
    11. {
    12. using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap"))
    13. {
    14. Mutex mutex = Mutex.OpenExisting("testmapmutex");
    15. mutex.WaitOne();
    16. using (MemoryMappedViewStream stream = mmf.CreateViewStream(1, 0))
    17. {
    18. BinaryWriter writer = new BinaryWriter(stream);
    19. writer.Write(0);
    20. }
    21. mutex.ReleaseMutex();
    22. }
    23. }
    24. catch (FileNotFoundException)
    25. {
    26. Console.WriteLine("Memory-mapped file does not exist. Run Process A first.");
    27. }
    28. }
    29. }

    Process C

    1. using System;
    2. using System.IO;
    3. using System.IO.MemoryMappedFiles;
    4. using System.Threading;
    5. class Program
    6. {
    7. // Process C:
    8. static void Main(string[] args)
    9. {
    10. try
    11. {
    12. using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap"))
    13. {
    14. Mutex mutex = Mutex.OpenExisting("testmapmutex");
    15. mutex.WaitOne();
    16. using (MemoryMappedViewStream stream = mmf.CreateViewStream(2, 0))
    17. {
    18. BinaryWriter writer = new BinaryWriter(stream);
    19. writer.Write(1);
    20. }
    21. mutex.ReleaseMutex();
    22. }
    23. }
    24. catch (FileNotFoundException)
    25. {
    26. Console.WriteLine("Memory-mapped file does not exist. Run Process A first, then B.");
    27. }
    28. }
    29. }
  • 相关阅读:
    leetcode 26
    电脑硬盘数据恢复一般需要收费多少钱
    web前端期末大作业网页设计与制作 ——汉口我的家乡旅游景点 5页HTML+CSS+JavaScript
    [Python]Django 模型
    L. Lemper Cooking Competition(前缀和/逆序对/树状数组/归并排序)
    3-8发送私信
    adb删除系统应用
    C#中数组、ArrayList与List对象的区别及使用场景
    小程序-防抖解决弹窗多次出现问题
    day39-网络编程01
  • 原文地址:https://blog.csdn.net/m0_51887793/article/details/134387562