封装:内存镜像文件(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

相关文章

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

JAVA封装多线程实现的方式及原理

《JAVA封装多线程实现的方式及原理》:本文主要介绍Java中封装多线程的原理和常见方式,通过封装可以简化多线程的使用,提高安全性,并增强代码的可维护性和可扩展性,需要的朋友可以参考下... 目录前言一、封装的目标二、常见的封装方式及原理总结前言在 Java 中,封装多线程的原理主要围绕着将多线程相关的操

Javascript访问Promise对象返回值的操作方法

《Javascript访问Promise对象返回值的操作方法》这篇文章介绍了如何在JavaScript中使用Promise对象来处理异步操作,通过使用fetch()方法和Promise对象,我们可以从... 目录在Javascript中,什么是Promise1- then() 链式操作2- 在之后的代码中使

Redis 内存淘汰策略深度解析(最新推荐)

《Redis内存淘汰策略深度解析(最新推荐)》本文详细探讨了Redis的内存淘汰策略、实现原理、适用场景及最佳实践,介绍了八种内存淘汰策略,包括noeviction、LRU、LFU、TTL、Rand... 目录一、 内存淘汰策略概述二、内存淘汰策略详解2.1 ​noeviction(不淘汰)​2.2 ​LR

Golang基于内存的键值存储缓存库go-cache

《Golang基于内存的键值存储缓存库go-cache》go-cache是一个内存中的key:valuestore/cache库,适用于单机应用程序,本文主要介绍了Golang基于内存的键值存储缓存库... 目录文档安装方法示例1示例2使用注意点优点缺点go-cache 和 Redis 缓存对比1)功能特性

如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件

《如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件》本文介绍了如何使用Docker部署FTP服务器和Nginx,并通过HTTP访问FTP中的文件,通过将FTP数据目录挂载到N... 目录docker部署FTP和Nginx并通过HTTP访问FTP里的文件1. 部署 FTP 服务器 (

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

CSS3 最强二维布局系统之Grid 网格布局

《CSS3最强二维布局系统之Grid网格布局》CS3的Grid网格布局是目前最强的二维布局系统,可以同时对列和行进行处理,将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,本文介... 深入学习 css3 目前最强大的布局系统 Grid 网格布局Grid 网格布局的基本认识Grid 网

golang内存对齐的项目实践

《golang内存对齐的项目实践》本文主要介绍了golang内存对齐的项目实践,内存对齐不仅有助于提高内存访问效率,还确保了与硬件接口的兼容性,是Go语言编程中不可忽视的重要优化手段,下面就来介绍一下... 目录一、结构体中的字段顺序与内存对齐二、内存对齐的原理与规则三、调整结构体字段顺序优化内存对齐四、内

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

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