封装:内存镜像文件(MemoryMappedFile)封装,一维二维三维定点访问

本文主要是介绍封装:内存镜像文件(MemoryMappedFile)封装,一维二维三维定点访问,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

MemoryMappedFile简介:



1、问题:公司底层限制使用32位应用程序,针对大数据数据模型会产生内存溢出;

2、解决方案:C#中可以使用MemoryMappedFile类将内存映射到硬盘中,将大网格数据保存在硬盘中节省内存;


3、实测结果:读取写入速度可以接受,持久化效果良好;

4、针对使用结构进行了如下封装,可用于泛型索引访问

    /// <summary> 泛型 T 内存映射文件 </summary>public partial class MmfEntity<T> where T : struct{#region - 成员变量 -private long _size;/// <summary> 文件大小 </summary>public long Size{get { return _size; }}/// <summary> 实体大小 </summary>public int MLeight{get { return Marshal.SizeOf(typeof(T)); }}private int _count;/// <summary> T 类型的数量 </summary>public int Count{get { return _count; }set{//  设置容器大小this._size = MLeight * value;_count = value;}}private string _file;/// <summary> 文件全路径 </summary>public string FileInf{get { return _file; }set { _file = value; }}private string _name;/// <summary> 镜像名称 </summary>public string Name{get{if (string.IsNullOrEmpty(_name)){_name = Path.GetFileNameWithoutExtension(_file);}return _name;}}private MemoryMappedFile _mmf;/// <summary> 内存镜像 </summary>protected MemoryMappedFile Mmf{get{BuildFile();return _mmf;}}/// <summary> 创建文件 </summary>void BuildFile(){if (File.Exists(this.FileInf)){this.Dispose();File.Delete(this.FileInf);}//  如果存在删除_mmf = MemoryMappedFile.CreateFromFile(this.FileInf, FileMode.OpenOrCreate, _name, _size, MemoryMappedFileAccess.ReadWriteExecute);}private MemoryMappedViewAccessor _mapView;/// <summary> 随机访问视图  </summary>protected MemoryMappedViewAccessor MapView{get{if (_mmf == null){this.BuildFile();}if (_mapView == null){_mapView = _mmf.CreateViewAccessor();}return _mapView;}}private MemoryMappedViewStream _mapStream;/// <summary> 按循序访问的流 </summary>protected MemoryMappedViewStream MapStream{get{if (_mmf == null){this.BuildFile();}if (_mapStream == null){_mapStream = _mmf.CreateViewStream();}return _mapStream;}private set { _mapStream = value; }}#endregion/// <summary> 将 T 类型的结构从访问器读取到提供的引用中 </summary>public T GetPostion(long position){T structure;MapView.Read<T>(position, out structure);return structure;}/// <summary> 读取指定索引处结构 </summary>public T GetIndex(int index){long postion = index * this.MLeight;return this.GetPostion(postion);}/// <summary> 将 T 类型的结构从访问器读取到 T 类型的数组中 </summary>public T[] GetPostion(int count, long position = 0){T[] arr = new T[count];MapView.ReadArray<T>(position, arr, 0, count);return arr;}/// <summary> 将 T 类型的结构从访问器读取到 T 类型的数组中 </summary>public T[] GetAll(long position = 0){T[] arr = new T[this._count];MapView.ReadArray<T>(position, arr, 0, this._count);return arr;}/// <summary> 将一个结构写入访问器 </summary>public void SetPosition(long position, T structure){MapView.Write<T>(position, ref structure);}/// <summary> 写入指定索引处结构 </summary>public void SetIndex(int index, T structure){long postion = index * this.MLeight;this.SetPosition(postion, structure);}/// <summary> 将结构从 T 类型的数组写入访问器 </summary>public void SetPosition(long position, T[] arr){MapView.WriteArray<T>(position, arr, 0, arr.Length);}/// <summary> 将结构从 T 类型的数组写入访问器 </summary>public void SetAll(T[] arr){MapView.WriteArray<T>(0, arr, 0, arr.Length);}/// <summary> 将结构从 T 类型的数组写入访问器 </summary>public void SetAll(T t){T[] arr = new T[this.Count];this.Count.DoCountWhile(l => arr[l] = t);MapView.WriteArray<T>(0, arr, 0, this.Count);}/// <summary> 重置大小 </summary>public void ReSetSize(int count){this._count = count;this._size = count * this.MLeight;this.BuildFile();}}partial class MmfEntity<T> : IDisposable{#region - 构造函数 -public MmfEntity(string fileFullPath, int Tcount){this._file = fileFullPath;this._count = Tcount;this._size = Tcount * this.MLeight;this._name = Path.GetFileNameWithoutExtension(fileFullPath);this.BuildFile();}#endregion#region - 资源释放 -private bool _isDisposed = false;~MmfEntity(){Dispose(false);}public void Dispose(){Dispose(true);GC.SuppressFinalize(this);}protected virtual void Dispose(bool disposing){if (!_isDisposed){if (disposing){if (this._mapView != null) this._mapView.Dispose();if (this._mmf != null) this._mmf.Dispose();}this._isDisposed = true;}}#endregion}

5、针对二维三维的定点结构访问

    /// <summary> 二维存储结构 </summary>public class DxyMmfEntity<T> : MmfEntity<T> where T : struct{#region - 构造函数 -public DxyMmfEntity(string fileFullPath, int Tcount): base(fileFullPath, Tcount){}#endregionprivate int _x;/// <summary> X维数 </summary>public int X{get { return _x; }set { _x = value; }}private int _y;/// <summary> Y维数 </summary>public int Y{get { return _y; }set { _y = value; }}public void Set(int x, int y, T value) {this.SetIndex(TranFucntion(x, y), value);}/// <summary> 获取指定二维值 </summary>public T Get(int x, int y){return this.GetIndex(TranFucntion(x, y));}Func<int, int, int> tranFucntion;/// <summary> 设置指定二维值 </summary>public Func<int, int, int> TranFucntion{get { return (x, y) => y * this.X + x; }}}


    /// <summary> 三维存储结构 </summary>public class DxyzMmfEntity<T> : MmfEntity<T> where T : struct{#region - 构造函数 -public DxyzMmfEntity(string fileFullPath, int x, int y, int z): base(fileFullPath, x * y * z){this.X = x;this.Y = y;this.Z = z;}/// <summary> 初始化方法 </summary>public void Init(int x, int y, int z){this.X = x;this.Y = y;this.Z = z;base.ReSetSize(x * y * z);}#endregionprivate int _x;/// <summary> X维数 </summary>public int X{get { return _x; }set { _x = value; }}private int _y;/// <summary> Y维数 </summary>public int Y{get { return _y; }set { _y = value; }}private int _z;/// <summary> Z方向维数 </summary>public int Z{get { return _z; }set { _z = value; }}public void Set(int x, int y, int z, T value){this.SetIndex(TranFucntion(x, y, z), value);}/// <summary> 获取指定二维值 </summary>public T Get(int x, int y, int z){return this.GetIndex(TranFucntion(x, y, z));}Func<int, int, int> tranFucntion;/// <summary> 设置指定二维值 </summary>public Func<int, int, int, int> TranFucntion{get { return (x, y, z) => z * this.X * this.Y + y * this.X + x; }}}

6,测试用例

 class Program{static void Main(string[] args){long offset = 0x10000000; // 256 megabyteslong length = 0x20000000; // 512 megabyteslong test = 800 * 1024;// 800 * 1024 * 1024;// 800MBDateTime time = DateTime.Now;Console.WriteLine("开始创建文件!");using (MmfEntity<int> mmf = new MmfEntity<int>(@"E:\ExtremelyLargeImage.data", 100)){for (int i = 0; i < 100; i++){mmf.SetIndex(i, i);}for (int i = 0; i < 100; i++){Console.WriteLine("执行:" + i + "  值:" + mmf.GetIndex(i));}
            }
           Console.WriteLine("完成!");Console.WriteLine("大小:" + test + "用时:" + (DateTime.Now - time).ToString());Console.Read();}
}

7、MemoryMappedFile结构图



这篇关于封装:内存镜像文件(MemoryMappedFile)封装,一维二维三维定点访问的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1005049

相关文章

本地搭建DeepSeek-R1、WebUI的完整过程及访问

《本地搭建DeepSeek-R1、WebUI的完整过程及访问》:本文主要介绍本地搭建DeepSeek-R1、WebUI的完整过程及访问的相关资料,DeepSeek-R1是一个开源的人工智能平台,主... 目录背景       搭建准备基础概念搭建过程访问对话测试总结背景       最近几年,人工智能技术

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

Ollama整合open-webui的步骤及访问

《Ollama整合open-webui的步骤及访问》:本文主要介绍如何通过源码方式安装OpenWebUI,并详细说明了安装步骤、环境要求以及第一次使用时的账号注册和模型选择过程,需要的朋友可以参考... 目录安装环境要求步骤访问选择PjrIUE模型开始对话总结 安装官方安装地址:https://docs.

Go语言利用泛型封装常见的Map操作

《Go语言利用泛型封装常见的Map操作》Go语言在1.18版本中引入了泛型,这是Go语言发展的一个重要里程碑,它极大地增强了语言的表达能力和灵活性,本文将通过泛型实现封装常见的Map操作,感... 目录什么是泛型泛型解决了什么问题Go泛型基于泛型的常见Map操作代码合集总结什么是泛型泛型是一种编程范式,允

Linux内存泄露的原因排查和解决方案(内存管理方法)

《Linux内存泄露的原因排查和解决方案(内存管理方法)》文章主要介绍了运维团队在Linux处理LB服务内存暴涨、内存报警问题的过程,从发现问题、排查原因到制定解决方案,并从中学习了Linux内存管理... 目录一、问题二、排查过程三、解决方案四、内存管理方法1)linux内存寻址2)Linux分页机制3)

解读静态资源访问static-locations和static-path-pattern

《解读静态资源访问static-locations和static-path-pattern》本文主要介绍了SpringBoot中静态资源的配置和访问方式,包括静态资源的默认前缀、默认地址、目录结构、访... 目录静态资源访问static-locations和static-path-pattern静态资源配置

Java循环创建对象内存溢出的解决方法

《Java循环创建对象内存溢出的解决方法》在Java中,如果在循环中不当地创建大量对象而不及时释放内存,很容易导致内存溢出(OutOfMemoryError),所以本文给大家介绍了Java循环创建对象... 目录问题1. 解决方案2. 示例代码2.1 原始版本(可能导致内存溢出)2.2 修改后的版本问题在

大数据小内存排序问题如何巧妙解决

《大数据小内存排序问题如何巧妙解决》文章介绍了大数据小内存排序的三种方法:数据库排序、分治法和位图法,数据库排序简单但速度慢,对设备要求高;分治法高效但实现复杂;位图法可读性差,但存储空间受限... 目录三种方法:方法概要数据库排序(http://www.chinasem.cn对数据库设备要求较高)分治法(常

Redis多种内存淘汰策略及配置技巧分享

《Redis多种内存淘汰策略及配置技巧分享》本文介绍了Redis内存满时的淘汰机制,包括内存淘汰机制的概念,Redis提供的8种淘汰策略(如noeviction、volatile-lru等)及其适用场... 目录前言一、什么是 Redis 的内存淘汰机制?二、Redis 内存淘汰策略1. pythonnoe

Java访问修饰符public、private、protected及默认访问权限详解

《Java访问修饰符public、private、protected及默认访问权限详解》:本文主要介绍Java访问修饰符public、private、protected及默认访问权限的相关资料,每... 目录前言1. public 访问修饰符特点:示例:适用场景:2. private 访问修饰符特点:示例: