C#内存映射文件学习总结
生活随笔
收集整理的這篇文章主要介紹了
C#内存映射文件学习总结
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
C#內(nèi)存映射文件學(xué)習(xí)
http://www.cnblogs.com/flyant/p/4443187.html內(nèi)存映射文件是由一個(gè)文件到進(jìn)程地址空間的映射。
? ? ? ? C#提供了允許應(yīng)用程序把文件映射到一個(gè)進(jìn)程的函(MemoryMappedFile.CreateOrOpen)。內(nèi)存映射文件與虛擬內(nèi)存有些類似,通過內(nèi)存映射文件可以保留一個(gè)地址空間的區(qū)域,同時(shí)將物理存儲器提交給此區(qū)域,只是內(nèi)存文件映射的物理存儲器來自一個(gè)已經(jīng)存在于磁盤上的文件,而非系統(tǒng)的頁文件,而且在對該文件進(jìn)行操作之前必須首先對文件進(jìn)行映射,就如同將整個(gè)文件從磁盤加載到內(nèi)存。由此可以看出,使用內(nèi)存映射文件處理存儲于磁盤上的文件時(shí),將不必再對文件執(zhí)行I/O操作,這意味著在對文件進(jìn)行處理時(shí)將不必再為文件申請并分配緩存,所有的文件緩存操作均由系統(tǒng)直接管理,由于取消了將文件數(shù)據(jù)加載到內(nèi)存、數(shù)據(jù)從內(nèi)存到文件的回寫以及釋放內(nèi)存塊等步驟,使得內(nèi)存映射文件在處理大數(shù)據(jù)量的文件時(shí)能起到相當(dāng)重要的作用。另外,實(shí)際工程中的系統(tǒng)往往需要在多個(gè)進(jìn)程之間共享數(shù)據(jù),如果數(shù)據(jù)量小,處理方法是靈活多變的,如果共享數(shù)據(jù)容量巨大,那么就需要借助于內(nèi)存映射文件來進(jìn)行。實(shí)際上,內(nèi)存映射文件正是解決本地多個(gè)進(jìn)程間數(shù)據(jù)共享的最有效方法。
? ? ? ? 共享內(nèi)存是內(nèi)存映射文件的一種特殊情況,內(nèi)存映射的是一塊內(nèi)存,而非磁盤上的文件。共享內(nèi)存的主語是進(jìn)程(Process),操作系統(tǒng)默認(rèn)會給每一個(gè)進(jìn)程分配一個(gè)內(nèi)存空間,每一個(gè)進(jìn)程只允許訪問操作系統(tǒng)分配給它的哪一段內(nèi)存,而不能訪問其他進(jìn)程的。而有時(shí)候需要在不同進(jìn)程之間訪問同一段內(nèi)存,怎么辦呢?操作系統(tǒng)給出了創(chuàng)建訪問共享內(nèi)存的API,需要共享內(nèi)存的進(jìn)程可以通過這一組定義好的API來訪問多個(gè)進(jìn)程之間共有的內(nèi)存,各個(gè)進(jìn)程訪問這一段內(nèi)存就像訪問一個(gè)硬盤上的文件一樣。而.Net 4.0中引入了System.IO.MemoryMappedFiles命名空間,這個(gè)命名空間的類對windows 共享內(nèi)存相關(guān)API做了封裝,使.Net程序員可以更方便的使用內(nèi)存映射文件。
內(nèi)存映射文件實(shí)現(xiàn)進(jìn)程間通訊
? ? ? ? 內(nèi)存映射文件是實(shí)現(xiàn)進(jìn)程通訊的又一種方法,我們可以通過共享剪貼板、共享物理文件來實(shí)現(xiàn)進(jìn)程間的數(shù)據(jù)共享,這里我們還可以通過內(nèi)存映射文件來實(shí)現(xiàn)共享,這樣,文件內(nèi)的數(shù)據(jù)就可以用內(nèi)存讀/寫指令來訪問,而不是用ReadFile和WriteFile這樣的I/O系統(tǒng)函數(shù),從而提高了文件存取速度。這種方式更加快捷高效,最適用于需要讀取文件并且對文件內(nèi)包含的信息做語法分析的應(yīng)用程序,如:對輸入文件進(jìn)行語法分析的彩色語法編輯器,編譯器等。這種數(shù)據(jù)共享是讓兩個(gè)或多個(gè)進(jìn)程映射同一文件映射對象的視圖,即它們在共享同一物理存儲頁。這樣,當(dāng)一個(gè)進(jìn)程向內(nèi)存映射文件的一個(gè)視圖寫入數(shù)據(jù)時(shí),其他的進(jìn)程立即在自己的視圖中看到變化。
注意:
? ? ? ? 對文件映射對象要使用同一名字。
? ? ? ? 是讓兩個(gè)或多個(gè)進(jìn)程映射同一文件映射對象的視圖,即它們在共享同一物理存儲頁。這樣,當(dāng)一個(gè)進(jìn)程向內(nèi)存映射文件的一個(gè)視圖寫入數(shù)據(jù)時(shí),其他的進(jìn)程立即在自己的視圖中看到變化。但要注意,對文件映射對象要使用同一名字。
?內(nèi)存映射文件使用實(shí)例:
1. ? ? ?在同一進(jìn)程內(nèi)同時(shí)讀寫同一內(nèi)存映射文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.MemoryMappedFiles;
namespace UseMMFInProcess
{
? ? public partial class frmMain : Form
? ? {
? ? ? ? public frmMain()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? ? ? CreateMemoryMapFile();
? ? ? ? }
? ? ? ? private const int FILE_SIZE = 512;
? ? ? ? /// <summary>
? ? ? ? /// 引用內(nèi)存映射文件
? ? ? ? /// </summary>
? ? ? ? private MemoryMappedFile memoryFile = null;
? ? ? ? /// <summary>
? ? ? ? /// 用于訪問內(nèi)存映射文件的存取對象
? ? ? ? /// </summary>
? ? ? ? private MemoryMappedViewAccessor accessor1, accessor2,accessor;
? ? ? ? /// <summary>
? ? ? ? /// 創(chuàng)建內(nèi)存映射文件
? ? ? ? /// </summary>
? ? ? ? private void CreateMemoryMapFile()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? { ? ??
? ? ? ? ? ? ? ? memoryFile = MemoryMappedFile.CreateFromFile("MyFile.dat", FileMode.OpenOrCreate, "MyFile", FILE_SIZE); ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? //訪問文件前半段
? ? ? ? ? ? ? ? accessor1 = memoryFile.CreateViewAccessor(0, FILE_SIZE / 2); ? ? ? ? ? ?
? ? ? ? ? ? ? ? //訪問文件后半段
? ? ? ? ? ? ? ? accessor2 = memoryFile.CreateViewAccessor(FILE_SIZE / 2, FILE_SIZE / 2); ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? //訪問全部文件
? ? ? ? ? ? ? ? accessor = memoryFile.CreateViewAccessor();
? ? ? ? ? ? ? ? //InitFileContent();
? ? ? ? ? ? ? ? lblInfo.Text = "內(nèi)存文件創(chuàng)建成功";
? ? ? ? ? ? ? ? ShowFileContents();
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? lblInfo.Text = ex.Message;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 關(guān)閉并釋放資源
? ? ? ? /// </summary>
? ? ? ? private void DisposeMemoryMapFile()
? ? ? ? {
? ? ? ? ? ? if (accessor1 != null)
? ? ? ? ? ? ? ? accessor1.Dispose();
? ? ? ? ? ? if (accessor2 != null)
? ? ? ? ? ? ? ? accessor2.Dispose();
? ? ? ? ? ? if (memoryFile != null)
? ? ? ? ? ? ? ? memoryFile.Dispose();
? ? ? ? }
? ? ? ? private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
? ? ? ? {
? ? ? ? ? ? DisposeMemoryMapFile();
? ? ? ? }
? ? ? ? private void btnWrite1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (textBox1.Text.Length == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? lblInfo.Text = "請輸入一個(gè)字符";
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? char[] chs = textBox1.Text.ToCharArray();
? ? ? ? ? ? char ch = chs[0];
? ? ? ? ? ?
? ? ? ? ? ? for (int i = 0; i < FILE_SIZE / 2; i += 2)
? ? ? ? ? ? ? ? accessor1.Write(i, ch);
? ? ? ? ? ??
? ? ? ? ? ? lblInfo.Text = "字符“" + ch + "”已寫到文件前半部份";
? ? ? ? ? ? ShowFileContents();
? ? ? ? }
? ? ? ? private void btnShow_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? ShowFileContents();
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 初始化文件內(nèi)容為可視的字符“0”
? ? ? ? /// </summary>
? ? ? ? private void InitFileContent()
? ? ? ? {
? ? ? ? ? ? for (int i = 0; i < FILE_SIZE; i += 2)?
? ? ? ? ? ? ? ? accessor.Write(i,'0');
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 顯示文件內(nèi)容
? ? ? ? /// </summary>
? ? ? ? private void ShowFileContents()
? ? ? ? {
? ? ? ? ? ? StringBuilder sb = new StringBuilder(FILE_SIZE);
? ? ? ? ? ? sb.Append("上半段內(nèi)容:\n");
? ? ? ? ? ? int j = 0;
? ? ? ? ? ? for (int i = 0; i < FILE_SIZE / 2; i += 2)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sb.Append("\t");
? ? ? ? ? ? ? ? char ch = accessor.ReadChar(i);
? ? ? ? ? ? ? ? sb.Append(j);
? ? ? ? ? ? ? ? sb.Append(":");
? ? ? ? ? ? ? ? sb.Append(ch);
? ? ? ? ? ? ? ? j++;
? ? ? ? ? ? }
? ? ? ? ? ? sb.Append("\n下半段內(nèi)容:\n");
? ? ? ? ? ? for (int i = FILE_SIZE / 2; i < FILE_SIZE; i += 2)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sb.Append("\t");
? ? ? ? ? ? ? ? char ch = accessor.ReadChar(i);
? ? ? ? ? ? ? ? sb.Append(j);
? ? ? ? ? ? ? ? sb.Append(":");
? ? ? ? ? ? ? ? sb.Append(ch);
? ? ? ? ? ? ? ? j++;
? ? ? ? ? ? }
? ? ? ? ? ? richTextBox1.Text = sb.ToString();
? ? ? ? }
? ? ? ? private void btnWrite2_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (textBox2.Text.Length == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? lblInfo.Text = "請輸入一個(gè)字符";
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? char[] chs = textBox2.Text.ToCharArray();
? ? ? ? ? ? char ch = chs[0];
? ? ? ? ? ? for (int i = 0; i < FILE_SIZE / 2; i += 2)
? ? ? ? ? ? ? ? accessor2.Write(i, ch);
? ? ? ? ? ? lblInfo.Text = "字符“" + ch + "”已寫到文件后半部份";
? ? ? ? ? ? ShowFileContents();
? ? ? ? }
? ? }
}
2. ? ? ?使用內(nèi)存映射文件在進(jìn)程間傳送值類型數(shù)據(jù)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UseMMFBetweenProcess
{
? ? /// <summary>
? ? /// 要共享的數(shù)據(jù)結(jié)構(gòu),注意,其成員不能是引用類型
? ? /// </summary>
? ? public struct MyStructure
? ? {
? ? ? ? public int IntValue
? ? ? ? {
? ? ? ? ? ? get;
? ? ? ? ? ? set;
? ? ? ? }
? ? ? ? public float FloatValue
? ? ? ? {
? ? ? ? ? ? get;
? ? ? ? ? ? set;
? ? ? ? }?
? ? }?
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.MemoryMappedFiles;
using System.IO;
namespace UseMMFBetweenProcess
{
? ? public partial class frmMain : Form
? ? {
? ? ? ? public frmMain()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? ? ? InitMemoryMappedFile();
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 內(nèi)存映射文件的容量
? ? ? ? /// </summary>
? ? ? ? private const int FileSize = 1024 * 1024;
? ? ? ? private MemoryMappedFile file = null;
? ? ? ? private MemoryMappedViewAccessor accessor = null;
? ? ? ? /// <summary>
? ? ? ? /// 初始化內(nèi)存映射文件
? ? ? ? /// </summary>
? ? ? ? private void InitMemoryMappedFile()
? ? ? ? {
? ? ? ? ? ? file = MemoryMappedFile.CreateOrOpen("UseMMFBetweenProcess", FileSize);
? ? ? ? ? ? accessor = file.CreateViewAccessor();
? ? ? ? ? ? lblInfo.Text = "內(nèi)存文件創(chuàng)建或連接成功"; ? ? ? ??
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 要共享的數(shù)據(jù)對象
? ? ? ? /// </summary>
? ? ? ? private MyStructure data;
? ? ? ? /// <summary>
? ? ? ? /// 顯示數(shù)據(jù)到窗體上
? ? ? ? /// </summary>
? ? ? ? private void ShowData()
? ? ? ? {
? ? ? ? ? ? textBox1.Text = data.IntValue.ToString();
? ? ? ? ? ? textBox2.Text = data.FloatValue.ToString();
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 根據(jù)用戶輸入更新數(shù)據(jù)
? ? ? ? /// </summary>
? ? ? ? private void UpdateData()
? ? ? ? {
? ? ? ? ? ? data.IntValue = int.Parse(textBox1.Text);
? ? ? ? ? ? data.FloatValue = float.Parse(textBox2.Text);
? ? ? ? }
? ? ? ? private void btnSave_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? UpdateData();
? ? ? ? ? ? ? ? accessor.Write<MyStructure>(0, ref data);
? ? ? ? ? ? ? ? lblInfo.Text = "數(shù)據(jù)已經(jīng)保存到內(nèi)存文件中";
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? lblInfo.Text = ex.Message;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? private void btnLoad_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? accessor.Read<MyStructure>(0, out data);
? ? ? ? ? ? ShowData();
? ? ? ? ? ? lblInfo.Text = "成功從內(nèi)存文件中提取了數(shù)據(jù)";
? ? ? ? }
? ? ? ? private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
? ? ? ? {
? ? ? ? ? ? if (accessor != null)
? ? ? ? ? ? ? ? accessor.Dispose();
? ? ? ? ? ? if (file != null)
? ? ? ? ? ? ? ? file.Dispose();
? ? ? ? }
? ? }
}
3. ? ? ?利用序列化技術(shù)通過內(nèi)存映射文件實(shí)現(xiàn)進(jìn)程通訊
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace UseMMFBetweenProcess2
{
? ? public partial class frmMain : Form
? ? {
? ? ? ? public frmMain()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? ? ? InitMemoryMappedFile();
? ? ? ? }
? ? ? ?
? ? ? ? /// <summary>
? ? ? ? /// 圖片
? ? ? ? /// </summary>
? ? ? ? private Image bmp
? ? ? ? {
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return pictureBox1.Image;
? ? ? ? ? ? }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? pictureBox1.Image = value;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ?
? ? ? ?/// <summary>
? ? ? ?/// 圖片說明
? ? ? ?/// </summary>
? ? ? ? private string info
? ? ? ? {
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return txtImageInfo.Text;
? ? ? ? ? ? }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? txtImageInfo.Text = value;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? private MemoryMappedFile memoryFile = null;
? ? ? ? private MemoryMappedViewStream stream = null;
? ? ? ? /// <summary>
? ? ? ? /// 最大容量:10M
? ? ? ? /// </summary>
? ? ? ? private const int FileSize = 1024 * 1024 * 10; ?
? ? ? ? /// <summary>
? ? ? ? /// 創(chuàng)建內(nèi)存映射文件,獲取其讀寫流
? ? ? ? /// </summary>
? ? ? ? private void InitMemoryMappedFile()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? memoryFile = MemoryMappedFile.CreateOrOpen("UseMMFBetweenProcess2", FileSize);
? ? ? ? ? ? stream = memoryFile.CreateViewStream();
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex )
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show(ex.Message);
? ? ? ? ? ? ? ? Close();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 釋放相關(guān)資源
? ? ? ? /// </summary>
? ? ? ? private void DisposeMemoryMappedFile()
? ? ? ? {
? ? ? ? ? ? if (stream != null)
? ? ? ? ? ? ? ? stream.Close();
? ? ? ? ? ? if (memoryFile != null)
? ? ? ? ? ? ? ? memoryFile.Dispose();
? ? ? ? }
? ? ? ? private void btnLoadPic_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? ChooseImageFile();
? ? ? ? }
? ? ? ? //選擇圖片
? ? ? ? private void ChooseImageFile()
? ? ? ? {
? ? ? ? ? ? if (openFileDialog1.ShowDialog() == DialogResult.OK)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? bmp = new Bitmap(openFileDialog1.FileName);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //根據(jù)用戶設(shè)定的信息創(chuàng)建對象
? ? ? ? private MyPic CreateMyPicObj()
? ? ? ? {
? ? ? ? ? ? MyPic obj = new MyPic();
? ? ? ? ? ? obj.pic = bmp;
? ? ? ? ? ? obj.picInfo = info;
? ? ? ? ? ? return obj;
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 將MyPic對象保存到內(nèi)存映射文件中
? ? ? ? /// </summary>
? ? ? ? private void SaveToMMF()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? MyPic obj = CreateMyPicObj();
? ? ? ? ? ? IFormatter formatter = new BinaryFormatter();
? ? ? ? ? ? stream.Seek(0, SeekOrigin.Begin);
? ? ? ? ? ? formatter.Serialize(stream, obj);
? ? ? ? ? ? MessageBox.Show("對象已保存到內(nèi)存映射文件中");
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show(ex.Message);
? ? ? ? ? ? }
? ? ? ? }
?private void LoadFromMMF()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ?// CreateMyPicObj();
? ? ? ? ? ? IFormatter formatter = new BinaryFormatter();
? ? ? ? ? ? stream.Seek(0, SeekOrigin.Begin);
? ? ? ? ? ? MyPic obj = ? formatter.Deserialize(stream) as MyPic;
? ? ? ? ? ? if (obj != null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? bmp = obj.pic;
? ? ? ? ? ? ? ? info = obj.picInfo;
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? ? catch (Exception ex)
? ? ? ? ? {
? ? ? ? ? ? ? MessageBox.Show(ex.Message);
? ? ? ? ? }
? ? ? ? }
? ? ? ? private void btnExit_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? Close();
? ? ? ? }
? ? ? ? private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
? ? ? ? {
? ? ? ? ? ? DisposeMemoryMappedFile();
? ? ? ? }
? ? ? ? private void btnSaveToMMF_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? SaveToMMF();
? ? ? ? }
? ? ? ? private void btnLoadFromMMF_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? LoadFromMMF();
? ? ? ? }
? ? }
}
========
C# 用內(nèi)存映射文件讀取大文件(.txt)
http://www.cnblogs.com/criedshy/archive/2010/06/13/1757826.html? 網(wǎng)上有好多這類的文章,大部分都是用C/C++寫的,也有部分C#寫的,都思想都是一樣的,調(diào)用win32 API。
? 至于什么是內(nèi)存映射文件,相信還是有好多人不知道是怎么一回事的,我也是偶然看window 核心編程了解到的。
? C# 讀取大文件的方法也是用的用StreamReader一次讀出來,再用MemoryStream放在內(nèi)存,再用StreamReader一行行的讀出來,速度也挺快的,16M的文本大概也就8秒左右,算起來差不多算快了。不過還是不能滿足大文件(我沒測試)。
string content = string.Empty;
? ? ? ? ? ? ? using (StreamReader sr = new StreamReader(op.FileName))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? content = sr.ReadToEnd();//一次性讀入內(nèi)存
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? MemoryStream ms = new MemoryStream(Encoding.GetEncoding("GB2312").GetBytes(content));//放入內(nèi)存流,以便逐行讀取
? ? ? ? ? ? ? ? long line = 0;
? ? ? ? ? ? ? ? using (StreamReader sr = new StreamReader(ms))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? while (sr.Peek() > -1)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? this.richTextBox1.AppendText(sr.ReadLine() + "\r\n");
? ? ? ? ? ? ? ? ? ? ? ? Application.DoEvents();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ?}
內(nèi)存映射文件概述
內(nèi)存文件映射也是Windows的一種內(nèi)存管理方法,提供了一個(gè)統(tǒng)一的內(nèi)存管理特征,使應(yīng)用程序可以通過內(nèi)存指針對磁盤上的文件進(jìn)行訪問,其過程就如同對加載了文件的內(nèi)存的訪問。通過文件映射這種使磁盤文件的全部或部分內(nèi)容與進(jìn)程虛擬地址空間的某個(gè)區(qū)域建立映射關(guān)聯(lián)的能力,可以直接對被映射的文件進(jìn)行訪問,而不必執(zhí)行文件I/O操作也無需對文件內(nèi)容進(jìn)行緩沖處理。內(nèi)存文件映射的這種特性是非常適合于用來管理大尺寸文件的。
在使用內(nèi)存映射文件進(jìn)行I/O處理時(shí),系統(tǒng)對數(shù)據(jù)的傳輸按頁面來進(jìn)行。至于內(nèi)部的所有內(nèi)存頁面則是由虛擬內(nèi)存管理器來負(fù)責(zé)管理,由其來決定內(nèi)存頁面何時(shí)被分頁到磁盤,哪些頁面應(yīng)該被釋放以便為其它進(jìn)程提供空閑空間,以及每個(gè)進(jìn)程可以擁有超出實(shí)際分配物理內(nèi)存之外的多少個(gè)頁面空間等等。由于虛擬內(nèi)存管理器是以一種統(tǒng)一的方式來處理所有磁盤I/O的(以頁面為單位對內(nèi)存數(shù)據(jù)進(jìn)行讀寫),因此這種優(yōu)化使其有能力以足夠快的速度來處理內(nèi)存操作。
使用內(nèi)存映射文件時(shí)所進(jìn)行的任何實(shí)際I/O交互都是在內(nèi)存中進(jìn)行并以標(biāo)準(zhǔn)的內(nèi)存地址形式來訪問。磁盤的周期性分頁也是由操作系統(tǒng)在后臺隱蔽實(shí)現(xiàn)的,對應(yīng)用程序而言是完全透明的。內(nèi)存映射文件的這種特性在進(jìn)行大文件的磁盤事務(wù)操作時(shí)將獲得很高的效益。
需要說明的是,在系統(tǒng)的正常的分頁操作過程中,內(nèi)存映射文件并非一成不變的,它將被定期更新。如果系統(tǒng)要使用的頁面目前正被某個(gè)內(nèi)存映射文件所占用,系統(tǒng)將釋放此頁面,如果頁面數(shù)據(jù)尚未保存,系統(tǒng)將在釋放頁面之前自動完成頁面數(shù)據(jù)到磁盤的寫入。
對于使用頁虛擬存儲管理的Windows操作系統(tǒng),內(nèi)存映射文件是其內(nèi)部已有的內(nèi)存管理組件的一個(gè)擴(kuò)充。由可執(zhí)行代碼頁面和數(shù)據(jù)頁面組成的應(yīng)用程序可根據(jù)需要由操作系統(tǒng)來將這些頁面換進(jìn)或換出內(nèi)存。如果內(nèi)存中的某個(gè)頁面不再需要,操作系統(tǒng)將撤消此頁面原擁用者對它的控制權(quán),并釋放該頁面以供其它進(jìn)程使用。只有在該頁面再次成為需求頁面時(shí),才會從磁盤上的可執(zhí)行文件重新讀入內(nèi)存。同樣地,當(dāng)一個(gè)進(jìn)程初始化啟動時(shí),內(nèi)存的頁面將用來存儲該應(yīng)用程序的靜態(tài)、動態(tài)數(shù)據(jù),一旦對它們的操作被提交,這些頁面也將被備份至系統(tǒng)的頁面文件,這與可執(zhí)行文件被用來備份執(zhí)行代碼頁面的過程是很類似的。圖1展示了代碼頁面和數(shù)據(jù)頁面在磁盤存儲器上的備份過程:
圖1 進(jìn)程的代碼頁、數(shù)據(jù)頁在磁盤存儲器上的備份
顯然,如果可以采取同一種方式來處理代碼和數(shù)據(jù)頁面,無疑將會提高程序的執(zhí)行效率,而內(nèi)存映射文件的使用恰恰可以滿足此需求。
對大文件的管理
內(nèi)存映射文件對象在關(guān)閉對象之前并沒有必要撤銷內(nèi)存映射文件的所有視圖。在對象被釋放之前,所有的臟頁面將自動寫入磁盤。通過 CloseHandle()關(guān)閉內(nèi)存映射文件對象,只是釋放該對象,如果內(nèi)存映射文件代表的是磁盤文件,那么還需要調(diào)用標(biāo)準(zhǔn)文件I/O函數(shù)來將其關(guān)閉。在處理大文件處理時(shí),內(nèi)存映射文件將表示出卓越的優(yōu)勢,只需要消耗極少的物理資源,對系統(tǒng)的影響微乎其微。下面先給出內(nèi)存映射文件的一般編程流程框圖:
圖2 使用內(nèi)存映射文件的一般流程
而在某些特殊行業(yè),經(jīng)常要面對十幾GB乃至幾十GB容量的巨型文件,而一個(gè)32位進(jìn)程所擁有的虛擬地址空間只有232 = 4GB,顯然不能一次將文件映像全部映射進(jìn)來。對于這種情況只能依次將大文件的各個(gè)部分映射到進(jìn)程中的一個(gè)較小的地址空間。這需要對上面的一般流程進(jìn)行適當(dāng)?shù)母?#xff1a;
1)映射文件開頭的映像。
2)對該映像進(jìn)行訪問。
3)取消此映像
4)映射一個(gè)從文件中的一個(gè)更深的位移開始的新映像。
5)重復(fù)步驟2,直到訪問完全部的文件數(shù)據(jù)。
? ?下面是用C#寫的代碼,大部分代碼轉(zhuǎn)自網(wǎng)上,自己在原來的基礎(chǔ)上改了一改。
C#內(nèi)存映射文件代碼
?
? ? ?經(jīng)過測試16M的文本4秒可以讀出來。
? ? ?現(xiàn)在是有兩個(gè)問題還沒有解決:
1.就是編碼的問題,用Unicode解碼的時(shí)候,文件大會很慢,而用ANSI和ASCII就很快。不知道為什么,望知情者告之。
2.怎么知道文件的編碼是什么?用win32 IsTestUnicode只能判斷兩種,而且還不保證是對。
========
c#實(shí)現(xiàn)內(nèi)存映射文件共享內(nèi)存
http://blog.csdn.net/wangtiewei/article/details/51112668 ?內(nèi)存映射文件是利用虛擬內(nèi)存把文件映射到進(jìn)程的地址空間中去,在此之后進(jìn)程操作文件,就像操作進(jìn)程空間里的地址一樣了,比如使用C語言的 memcpy等內(nèi)存操作的函數(shù)。這種方法能夠很好的應(yīng)用在需要頻繁處理一個(gè)文件或者是一個(gè)大文件的場合,這種方式處理IO效率比普通IO效率要高
共享內(nèi)存是內(nèi)存映射文件的一種特殊情況,內(nèi)存映射的是一塊內(nèi)存,而非磁盤上的文件。共享內(nèi)存的主語是進(jìn)程(Process),操作系統(tǒng)默認(rèn)會給每一 個(gè)進(jìn)程分配一個(gè)內(nèi)存空間,每一個(gè)進(jìn)程只允許訪問操作系統(tǒng)分配給它的哪一段內(nèi)存,而不能訪問其他進(jìn)程的。而有時(shí)候需要在不同進(jìn)程之間訪問同一段內(nèi)存,怎么辦 呢?操作系統(tǒng)給出了創(chuàng)建訪問共享內(nèi)存的API,需要共享內(nèi)存的進(jìn)程可以通過這一組定義好的API來訪問多個(gè)進(jìn)程之間共有的內(nèi)存,各個(gè)進(jìn)程訪問這一段內(nèi)存就 像訪問一個(gè)硬盤上的文件一樣。而.Net 4.0中引入了System.IO. MemoryMappedFiles命名空間,這個(gè)命名空間的類對windows 共享內(nèi)存相關(guān)API做了封裝,使.Net程序員可以更方便的使用內(nèi)存映射文件。
在C#中使用共享內(nèi)存。以下App1的代碼讓用戶輸入一行文本到共享內(nèi)存中;App2不停的刷新控制臺,輸出最新的共享內(nèi)存內(nèi)容;App3實(shí)現(xiàn)的功能和App2相同,但讀取方法不同。
App1代碼: ?
using System; ?
using System.Collections.Generic;android從資源文件中讀取文件流顯示 ?
using System.Linq; ?
using System.Text; ?
??
using System.IO; ?
??
//引用內(nèi)存映射文件命名空間 ?
using System.IO.MemoryMappedFiles; ?
??
namespace App1 ?
{ ?
? ? class Program ?
? ? { ?
? ? ? ? static void Main(string[] args) ?
? ? ? ? { ?
? ? ? ? ? ? long capacity = 1<<10<<10; ?
??
? ? ? ? ? ? //創(chuàng)建或者打開共享內(nèi)存 ?
? ? ? ? ? ? using (var mmf = MemoryMappedFile.CreateOrOpen("testMmf", capacity, MemoryMappedFileAccess.ReadWrite)) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? //通過MemoryMappedFile的CreateViewAccssor方法獲得共享內(nèi)存的訪問器 ?
? ? ? ? ? ? ? ? var viewAccessor = mmf.CreateViewAccessor(0, capacity); ?
? ? ? ? ? ? ? ? //循環(huán)寫入,使在這個(gè)進(jìn)程中可以向共享內(nèi)存中寫入不同的字符串值 ?
? ? ? ? ? ? ? ? while (true) ?
? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? Console.WriteLine("請輸入一行要寫入共享內(nèi)存的文字:"); ?
??
? ? ? ? ? ? ? ? ? ? string input = Console.ReadLine(); ?
??
? ? ? ? ? ? ? ? ? ? //向共享內(nèi)存開始位置寫入字符串的長度 ?
? ? ? ? ? ? ? ? ? ? viewAccessor.Write(0, input.Length); ?
??
? ? ? ? ? ? ? ? ? ? //向共享內(nèi)存4位置寫入字符 ?
? ? ? ? ? ? ? ? ? ? viewAccessor.WriteArray<char>(4, input.ToArray(), 0, input.Length); ?
? ? ? ? ? ? ? ? } ?
??
? ? ? ? ? ? } ?
? ? ? ? ? ? ??
? ? ? ? } ?
? ? } ?
} ?
App2代碼: ?
using System; ?
using System.Collections.Generic; ?
using System.Linq; ?
using System.Text; ?
using System.Threading; ?
??
//引用使用內(nèi)存映射文件需要的命名空間 ?
using System.IO.MemoryMappedFiles; ?
??
namespace App2 ?
{ ?
? ? class Program ?
? ? { ?
? ? ? ? static void Main(string[] args) ?
? ? ? ? { ?
? ? ? ? ? ? ? long capacity = 1<<10<<10; ?
??
? ? ? ? ? ? ? using (var mmf = MemoryMappedFile.OpenExisting("testMmf")) ?
? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? MemoryMappedViewAccessor viewAccessor = mmf.CreateViewAccessor(0, capacity); ?
??
? ? ? ? ? ? ? ? ? //循環(huán)刷新共享內(nèi)存字符串的值 ?
? ? ? ? ? ? ? ? ? while (true) ?
? ? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? ? //讀取字符長度 ?
? ? ? ? ? ? ? ? ? ? ? int strLength = viewAccessor.ReadInt32(0); ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? char[] charsInMMf = new char[strLength]; ?
? ? ? ? ? ? ? ? ? ? ? //讀取字符 ?
? ? ? ? ? ? ? ? ? ? ? viewAccessor.ReadArray<char>(4, charsInMMf, 0, strLength); ?
? ? ? ? ? ? ? ? ? ? ? Console.Clear(); ?
? ? ? ? ? ? ? ? ? ? ? Console.Write(charsInMMf); ?
? ? ? ? ? ? ? ? ? ? ? Console.Write("\r"); ?
? ? ? ? ? ? ? ? ? ? ? Thread.Sleep(200); ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? } ?
? ? ? ? } ?
? ? } ?
} ?
App3代碼: ?
using System; ?
using System.Collections.Generic; ?
using System.Linq; ?
using System.Text; ?
??
using System.IO.MemoryMappedFiles; ?
using System.IO; ?
??
namespace App3 ?
{ ?
? ? class Program ?
? ? { ?
? ? ? ? static void Main(string[] args) ?
? ? ? ? { ?
? ? ? ? ? ? long capacity = 1 << 10 << 10; ?
? ? ? ? ? ? //打開共享內(nèi)存 ?
? ? ? ? ? ? using (var mmf = MemoryMappedFile.OpenExisting("testMmf")) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? //使用CreateViewStream方法返回stream實(shí)例 ?
? ? ? ? ? ? ? ? using (var mmViewStream = mmf.CreateViewStream(0, capacity)) ?
? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? //這里要制定Unicode編碼否則會出問題 ?
? ? ? ? ? ? ? ? ? ? using (BinaryReader rdr = new BinaryReader(mmViewStream,Encoding.Unicode)) ?
? ? ? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? ? ? while (true) ?
? ? ? ? ? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? mmViewStream.Seek(0, SeekOrigin.Begin); ?
??
? ? ? ? ? ? ? ? ? ? ? ? ? ? int length = rdr.ReadInt32(); ?
??
? ? ? ? ? ? ? ? ? ? ? ? ? ? char[] chars = rdr.ReadChars(length); ?
??
? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.Write(chars); ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.Write("\r"); ?
??
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.Threading.Thread.Sleep(200); ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.Clear(); ?
? ? ? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? } ?
} ?
在讀數(shù)據(jù)時(shí)用了2種方法。
因?yàn)樵谥昂苌贂玫竭M(jìn)程之間的通信,所以此方法只是想初步的認(rèn)識下。此程序?qū)懙倪^于簡陋,有很多東西都沒有去判斷。比如說是怎么創(chuàng)建了一個(gè)共享內(nèi)存怎么取刪除它等等。。。
========
用C#實(shí)現(xiàn)的內(nèi)存映射
http://blog.csdn.net/linux7985/article/details/5853358當(dāng)文件過大時(shí),無法一次性載入內(nèi)存時(shí),就需要分次,分段的載入文件
主要是用了以下的WinAPI
LPVOID MapViewOfFile(HANDLE hFileMappingObject,
DWORD dwDesiredAccess,
DWORD dwFileOffsetHigh,
DWORD dwFileOffsetLow,
DWORD dwNumberOfBytesToMap);
?
MapViewOfFile() 函數(shù)負(fù)責(zé)把文件數(shù)據(jù)映射到進(jìn)程的地址空間,參數(shù)hFileMappingObject 為 CreateFileMapping()返回的文件映像對象句柄。參數(shù)dwDesiredAccess則再次指定了對文件數(shù)據(jù)的訪問方式,而且同樣要與 CreateFileMapping()函數(shù)所設(shè)置的保護(hù)屬性相匹配。雖然這里一再對保護(hù)屬性進(jìn)行重復(fù)設(shè)置看似多余,但卻可以使應(yīng)用程序能更多的對數(shù)據(jù)的保護(hù)屬性實(shí)行有效控制。MapViewOfFile()函數(shù)允許全部或部分映射文件,在映射時(shí),需要指定數(shù)據(jù)文件的偏移地址以及待映射的長度。其中,文件的偏移地址由DWORD型的參數(shù)dwFileOffsetHigh和dwFileOffsetLow組成的64位值來指定,而且必須是操作系統(tǒng)的分配粒度的整數(shù)倍,對于Windows操作系統(tǒng),分配粒度固定為64KB。當(dāng)然,也可以通過如下代碼來動態(tài)獲取當(dāng)前操作系統(tǒng)的分配粒度:
SYSTEM_INFO sinf;
GetSystemInfo(&sinf);
DWORD dwAllocationGranularity = sinf.dwAllocationGranularity;
參數(shù)dwNumberOfBytesToMap指定了數(shù)據(jù)文件的映射長度,這里需要特別指出的是,對于Windows 9x操作系統(tǒng),如果MapViewOfFile()無法找到足夠大的區(qū)域來存放整個(gè)文件映射對象,將返回空值(NULL);但是在Windows 2000下,MapViewOfFile()只需要為必要的視圖找到足夠大的一個(gè)區(qū)域即可,而無須考慮整個(gè)文件映射對象的大小。
由此看出,分頁映射文件時(shí),每頁的起始位置startpos,必須為64K的整數(shù)倍。
以下貼出源代碼,防止忘記了
using System; ?
using System.Collections.Generic; ?
using System.Text; ?
using System.Runtime.InteropServices; ?
??
namespace BlueVision.SaYuan.FileMapping ?
{ ?
? ? public class ShareMemory ?
? ? { ?
? ? ? ? [DllImport( "user32.dll", CharSet = CharSet.Auto )] ?
? ? ? ? public static extern IntPtr SendMessage( IntPtr hWnd, int Msg, int wParam, IntPtr lParam ); ?
??
? ? ? ? [DllImport( "Kernel32.dll", CharSet = CharSet.Auto )] ?
? ? ? ? public static extern IntPtr CreateFileMapping( IntPtr hFile, IntPtr lpAttributes, uint flProtect, uint dwMaxSizeHi, uint dwMaxSizeLow, string lpName ); ?
??
? ? ? ? [DllImport( "Kernel32.dll", CharSet = CharSet.Auto )] ?
? ? ? ? public static extern IntPtr OpenFileMapping( int dwDesiredAccess, [MarshalAs( UnmanagedType.Bool )] bool bInheritHandle, string lpName ); ?
??
? ? ? ? [DllImport( "Kernel32.dll", CharSet = CharSet.Auto )] ?
? ? ? ? public static extern IntPtr MapViewOfFile( IntPtr hFileMapping, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap ); ?
??
? ? ? ? [DllImport( "Kernel32.dll", CharSet = CharSet.Auto )] ?
? ? ? ? public static extern bool UnmapViewOfFile( IntPtr pvBaseAddress ); ?
??
? ? ? ? [DllImport( "Kernel32.dll", CharSet = CharSet.Auto )] ?
? ? ? ? public static extern bool CloseHandle( IntPtr handle ); ?
??
? ? ? ? [DllImport( "kernel32", EntryPoint = "GetLastError" )] ?
? ? ? ? public static extern int GetLastError(); ?
??
? ? ? ? [DllImport( "kernel32.dll" )] ?
? ? ? ? static extern void GetSystemInfo( out SYSTEM_INFO lpSystemInfo ); ?
??
? ? ? ? [StructLayout( LayoutKind.Sequential )] ?
? ? ? ? public struct SYSTEM_INFO ?
? ? ? ? { ?
? ? ? ? ? ? public ushort processorArchitecture; ?
? ? ? ? ? ? ushort reserved; ?
? ? ? ? ? ? public uint pageSize; ?
? ? ? ? ? ? public IntPtr minimumApplicationAddress; ?
? ? ? ? ? ? public IntPtr maximumApplicationAddress; ?
? ? ? ? ? ? public IntPtr activeProcessorMask; ?
? ? ? ? ? ? public uint numberOfProcessors; ?
? ? ? ? ? ? public uint processorType; ?
? ? ? ? ? ? public uint allocationGranularity; ?
? ? ? ? ? ? public ushort processorLevel; ?
? ? ? ? ? ? public ushort processorRevision; ?
? ? ? ? } ?
? ? ? ? /// <summary> ?
? ? ? ? /// 獲取系統(tǒng)的分配粒度 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? public static uint GetPartitionsize() ?
? ? ? ? { ?
? ? ? ? ? ? SYSTEM_INFO sysInfo; ?
? ? ? ? ? ? GetSystemInfo( out sysInfo ); ?
? ? ? ? ? ? return sysInfo.allocationGranularity; ?
? ? ? ? } ?
??
? ? ? ? const int ERROR_ALREADY_EXISTS = 183; ?
??
? ? ? ? const int FILE_MAP_COPY = 0x0001; ?
? ? ? ? const int FILE_MAP_WRITE = 0x0002; ?
? ? ? ? const int FILE_MAP_READ = 0x0004; ?
? ? ? ? const int FILE_MAP_ALL_ACCESS = 0x0002 | 0x0004; ?
??
? ? ? ? const int PAGE_READONLY = 0x02; ?
? ? ? ? const int PAGE_READWRITE = 0x04; ?
? ? ? ? const int PAGE_WRITECOPY = 0x08; ?
? ? ? ? const int PAGE_EXECUTE = 0x10; ?
? ? ? ? const int PAGE_EXECUTE_READ = 0x20; ?
? ? ? ? const int PAGE_EXECUTE_READWRITE = 0x40; ?
??
? ? ? ? const int SEC_COMMIT = 0x8000000; ?
? ? ? ? const int SEC_IMAGE = 0x1000000; ?
? ? ? ? const int SEC_NOCACHE = 0x10000000; ?
? ? ? ? const int SEC_RESERVE = 0x4000000; ?
??
? ? ? ? IntPtr m_fHandle; ?
??
? ? ? ? IntPtr m_hSharedMemoryFile = IntPtr.Zero; ?
? ? ? ? IntPtr m_pwData = IntPtr.Zero; ?
? ? ? ? bool m_bAlreadyExist = false; ?
? ? ? ? bool m_bInit = false; ?
? ? ? ? uint m_MemSize = 0x1400000;//20M ?
? ? ? ? long m_offsetBegin = 0; ?
? ? ? ? long m_FileSize = 0; ?
? ? ? ? FileReader File = new FileReader(); ?
? ??
? ? ? ? /// <summary> ?
? ? ? ? /// ?初始化文件 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="MemSize">緩沖大小</param> ?
? ? ? ? public ShareMemory( string filename, uint memSize ) ?
? ? ? ? { ?
? ? ? ? ? ? // 分頁映射文件時(shí),每頁的起始位置startpos,必須為64K的整數(shù)倍。 ?
? ? ? ? ? ? // memSize即緩存區(qū)的大小必須是系統(tǒng)分配粒度的整倍說,window系統(tǒng)的分配粒度是64KB ?
? ? ? ? ? ? this.m_MemSize = memSize; ?
? ? ? ? ? ? Init( filename ); ?
? ? ? ? } ?
? ??
? ? ? ? /// <summary> ?
? ? ? ? /// 默認(rèn)映射20M緩沖 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="filename"></param> ?
? ? ? ? public ShareMemory( string filename ) ?
? ? ? ? { ?
? ? ? ? ? ? this.m_MemSize = 0x1400000; ?
? ? ? ? ? ? Init( filename ); ?
? ? ? ? } ?
??
? ? ? ? ~ShareMemory() ?
? ? ? ? { ?
? ? ? ? ? ? Close(); ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 初始化共享內(nèi)存 ?
? ? ? ? /// ??
? ? ? ? /// 共享內(nèi)存名稱 ?
? ? ? ? /// 共享內(nèi)存大小 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="strName"></param> ?
? ? ? ? protected void Init( string strName ) ?
? ? ? ? { ?
? ? ? ? ? ? //if (lngSize <= 0 || lngSize > 0x00800000) lngSize = 0x00800000; ?
??
? ? ? ? ? ? if ( !System.IO.File.Exists( strName ) ) throw new Exception( "未找到文件" ); ?
??
? ? ? ? ? ? System.IO.FileInfo f = new System.IO.FileInfo( strName ); ? ?
? ? ? ? ? ? m_FileSize = f.Length; ? ?
? ? ? ? ? ? m_fHandle = File.Open( strName ); ?
??
? ? ? ? ? ? if ( strName.Length > 0 ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? //創(chuàng)建文件映射 ?
? ? ? ? ? ? ? ? m_hSharedMemoryFile = CreateFileMapping( m_fHandle, IntPtr.Zero, ( uint )PAGE_READONLY, 0, ( uint )m_FileSize, "mdata" ); ?
? ? ? ? ? ? ? ? if ( m_hSharedMemoryFile == IntPtr.Zero ) ?
? ? ? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? ? m_bAlreadyExist = false; ?
? ? ? ? ? ? ? ? ? ? m_bInit = false; ?
? ? ? ? ? ? ? ? ? ? throw new Exception( "CreateFileMapping失敗LastError=" + GetLastError().ToString() ); ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? else ?
? ? ? ? ? ? ? ? ? ? m_bInit = true; ?
??
? ? ? ? ? ? ? ? 映射第一塊文件 ?
? ? ? ? ? ? ? ? //m_pwData = MapViewOfFile(m_hSharedMemoryFile, FILE_MAP_READ, 0, 0, (uint)m_MemSize); ?
? ? ? ? ? ? ? ? //if (m_pwData == IntPtr.Zero) ?
? ? ? ? ? ? ? ? //{ ?
? ? ? ? ? ? ? ? // ? ?m_bInit = false; ?
? ? ? ? ? ? ? ? // ? ?throw new Exception("m_hSharedMemoryFile失敗LastError=" + GetLastError().ToString()); ?
? ? ? ? ? ? ? ? //} ?
??
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? /// <summary> ?
? ? ? ? /// 獲取高32位 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="intValue"></param> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? private static uint GetHighWord( UInt64 intValue ) ?
? ? ? ? { ?
? ? ? ? ? ? return Convert.ToUInt32( intValue >> 32 ); ?
? ? ? ? } ?
? ? ? ? /// <summary> ?
? ? ? ? /// 獲取低32位 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="intValue"></param> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? private static uint GetLowWord( UInt64 intValue ) ?
? ? ? ? { ?
??
? ? ? ? ? ? return Convert.ToUInt32( intValue & 0x00000000FFFFFFFF ); ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 獲取下一個(gè)文件塊 塊大小為20M ?
? ? ? ? /// </summary> ?
? ? ? ? /// <returns>false 表示已經(jīng)是最后一塊文件</returns> ?
? ? ? ? public uint GetNextblock() ?
? ? ? ? { ?
? ? ? ? ? ? if ( !this.m_bInit ) throw new Exception( "文件未初始化。" ); ?
? ? ? ? ? ? //if ( m_offsetBegin + m_MemSize >= m_FileSize ) return false; ?
??
? ? ? ? ? ? uint m_Size = GetMemberSize(); ?
? ? ? ? ? ? if ( m_Size == 0 ) return m_Size; ?
??
? ? ? ? ? ? // 更改緩沖區(qū)大小 ?
? ? ? ? ? ? m_MemSize = m_Size; ?
??
? ? ? ? ? ? //卸載前一個(gè)文件 ?
? ? ? ? ? ? //bool l_result = UnmapViewOfFile( m_pwData ); ?
? ? ? ? ? ? //m_pwData = IntPtr.Zero; ?
??
??
? ? ? ? ? ? m_pwData = MapViewOfFile( m_hSharedMemoryFile, FILE_MAP_READ, GetHighWord( ( UInt64 )m_offsetBegin ), GetLowWord( ( UInt64 )m_offsetBegin ), m_Size ); ?
? ? ? ? ? ? if ( m_pwData == IntPtr.Zero ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? m_bInit = false; ?
? ? ? ? ? ? ? ? throw new Exception( "映射文件塊失敗" + GetLastError().ToString() ); ?
? ? ? ? ? ? } ?
? ? ? ? ? ? m_offsetBegin = m_offsetBegin + m_Size; ?
??
? ? ? ? ? ? return m_Size; //創(chuàng)建成功 ?
? ? ? ? } ?
? ? ? ? /// <summary> ?
? ? ? ? /// 返回映射區(qū)大小 ?
? ? ? ? /// </summary> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? private uint GetMemberSize() ?
? ? ? ? { ?
? ? ? ? ? ? if ( m_offsetBegin >= m_FileSize ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? return 0; ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else if ( m_offsetBegin + m_MemSize >= m_FileSize ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? long temp = m_FileSize - m_offsetBegin; ?
? ? ? ? ? ? ? ? return ( uint )temp; ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ? ? ? ? ? ? ? return m_MemSize; ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 關(guān)閉內(nèi)存映射 ?
? ? ? ? /// </summary> ?
? ? ? ? public void Close() ?
? ? ? ? { ?
? ? ? ? ? ? if ( m_bInit ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? UnmapViewOfFile( m_pwData ); ?
? ? ? ? ? ? ? ? CloseHandle( m_hSharedMemoryFile ); ?
? ? ? ? ? ? ? ? File.Close(); ?
? ? ? ? ? ? } ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 從當(dāng)前塊中獲取數(shù)據(jù) ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="bytData">數(shù)據(jù)</param> ?
? ? ? ? /// <param name="lngAddr">起始數(shù)據(jù)</param> ?
? ? ? ? /// <param name="lngSize">數(shù)據(jù)長度,最大值=緩沖長度</param> ?
? ? ? ? /// <param name="Unmap">讀取完成是否卸載緩沖區(qū)</param> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? public void Read( ref byte[] bytData, int lngAddr, int lngSize, bool Unmap ) ?
? ? ? ? { ?
? ? ? ? ? ? if ( lngAddr + lngSize > m_MemSize ) ?
? ? ? ? ? ? ? ? throw new Exception( "Read操作超出數(shù)據(jù)區(qū)" ); ?
? ? ? ? ? ? if ( m_bInit ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? // string bb = Marshal.PtrToStringAuto(m_pwData);// ?
? ? ? ? ? ? ? ? Marshal.Copy( m_pwData, bytData, lngAddr, lngSize ); ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? throw new Exception( "文件未初始化" ); ?
? ? ? ? ? ? } ?
??
? ? ? ? ? ? if ( Unmap ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? bool l_result = UnmapViewOfFile( m_pwData ); ?
? ? ? ? ? ? ? ? if ( l_result ) ?
? ? ? ? ? ? ? ? ? ? m_pwData = IntPtr.Zero; ?
? ? ? ? ? ? } ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 從當(dāng)前塊中獲取數(shù)據(jù) ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="bytData">數(shù)據(jù)</param> ?
? ? ? ? /// <param name="lngAddr">起始數(shù)據(jù)</param> ?
? ? ? ? /// <param name="lngSize">數(shù)據(jù)長度,最大值=緩沖長度</param> ?
? ? ? ? /// <exception cref="Exception: Read操作超出數(shù)據(jù)區(qū)"></exception> ?
? ? ? ? /// <exception cref="Exception: 文件未初始化"></exception> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? public void Read( ref byte[] bytData, int lngAddr, int lngSize ) ?
? ? ? ? { ?
? ? ? ? ? ? if ( lngAddr + lngSize > m_MemSize ) ?
? ? ? ? ? ? ? ? throw new Exception( "Read操作超出數(shù)據(jù)區(qū)" ); ?
? ? ? ? ? ? if ( m_bInit ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? Marshal.Copy( m_pwData, bytData, lngAddr, lngSize ); ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? throw new Exception( "文件未初始化" ); ?
? ? ? ? ? ? } ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 從當(dāng)前塊中獲取數(shù)據(jù) ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="lngAddr">緩存區(qū)偏移量</param> ?
? ? ? ? /// <param name="byteData">數(shù)據(jù)數(shù)組</param> ?
? ? ? ? /// <param name="StartIndex">數(shù)據(jù)數(shù)組開始復(fù)制的下標(biāo)</param> ?
? ? ? ? /// <param name="lngSize">數(shù)據(jù)長度,最大值=緩沖長度</param> ?
? ? ? ? /// <exception cref="Exception: 起始數(shù)據(jù)超過緩沖區(qū)長度"></exception> ?
? ? ? ? /// <exception cref="Exception: 文件未初始化"></exception> ?
? ? ? ? /// <returns>返回實(shí)際讀取值</returns> ?
? ? ? ? public uint ReadBytes( int lngAddr, ref byte[] byteData, int StartIndex, uint intSize ) ?
? ? ? ? { ?
? ? ? ? ? ? if ( lngAddr >= m_MemSize ) ?
? ? ? ? ? ? ? ? throw new Exception( "起始數(shù)據(jù)超過緩沖區(qū)長度" ); ?
??
? ? ? ? ? ? if ( lngAddr + intSize > m_MemSize ) ?
? ? ? ? ? ? ? ? intSize = m_MemSize - ( uint )lngAddr; ?
??
? ? ? ? ? ? if ( m_bInit ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? IntPtr s = new IntPtr( ( long )m_pwData + lngAddr ); // 地址偏移 ?
? ? ? ? ? ? ? ? Marshal.Copy( s, byteData, StartIndex, ( int )intSize ); ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? throw new Exception( "文件未初始化" ); ?
? ? ? ? ? ? } ?
??
? ? ? ? ? ? return intSize; ?
? ? ? ? } ?
??
? ? ? ? /// <summary> ?
? ? ? ? /// 寫數(shù)據(jù) ?
? ? ? ? /// </summary> ?
? ? ? ? /// <param name="bytData">數(shù)據(jù)</param> ?
? ? ? ? /// <param name="lngAddr">起始地址</param> ?
? ? ? ? /// <param name="lngSize">個(gè)數(shù)</param> ?
? ? ? ? /// <returns></returns> ?
? ? ? ? private int Write( byte[] bytData, int lngAddr, int lngSize ) ?
? ? ? ? { ?
? ? ? ? ? ? if ( lngAddr + lngSize > m_MemSize ) return 2; //超出數(shù)據(jù)區(qū) ?
? ? ? ? ? ? if ( m_bInit ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? Marshal.Copy( bytData, lngAddr, m_pwData, lngSize ); ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? return 1; //共享內(nèi)存未初始化 ?
? ? ? ? ? ? } ?
? ? ? ? ? ? return 0; //寫成功 ?
? ? ? ? } ?
? ? } ?
? ? internal class FileReader ?
? ? { ?
? ? ? ? const uint GENERIC_READ = 0x80000000; ?
? ? ? ? const uint OPEN_EXISTING = 3; ?
? ? ? ? System.IntPtr handle; ?
??
? ? ? ? [DllImport( "kernel32", SetLastError = true )] ?
? ? ? ? public static extern System.IntPtr CreateFile( ?
? ? ? ? ? ? string FileName, ? ? ? ? ?// file name ?
? ? ? ? ? ? uint DesiredAccess, ? ? ? // access mode ?
? ? ? ? ? ? uint ShareMode, ? ? ? ? ? // share mode ?
? ? ? ? ? ? uint SecurityAttributes, ?// Security Attributes ?
? ? ? ? ? ? uint CreationDisposition, // how to create ?
? ? ? ? ? ? uint FlagsAndAttributes, ?// file attributes ?
? ? ? ? ? ? int hTemplateFile ? ? ? ? // handle to template file ?
? ? ? ? ); ?
??
? ? ? ? [System.Runtime.InteropServices.DllImport( "kernel32", SetLastError = true )] ?
? ? ? ? static extern bool CloseHandle ?
? ? ? ? ( ?
? ? ? ? ? ? System.IntPtr hObject // handle to object ?
? ? ? ? );?
? ? ? ? public IntPtr Open( string FileName ) ?
? ? ? ? { ?
? ? ? ? ? ? // open the existing file for reading ? ? ? ??
? ? ? ? ? ? handle = CreateFile ?
? ? ? ? ? ? ( ?
? ? ? ? ? ? ? ? FileName, ?
? ? ? ? ? ? ? ? GENERIC_READ, ?
? ? ? ? ? ? ? ? 0, ?
? ? ? ? ? ? ? ? 0, ?
? ? ? ? ? ? ? ? OPEN_EXISTING, ?
? ? ? ? ? ? ? ? 0, ?
? ? ? ? ? ? ? ? 0 ?
? ? ? ? ? ? ); ?
??
? ? ? ? ? ? if ( handle != System.IntPtr.Zero ) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? return handle; ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? throw new Exception( "打開文件失敗" ); ?
? ? ? ? ? ? } ?
? ? ? ? } ?
??
? ? ? ? public bool Close() ?
? ? ? ? { ?
? ? ? ? ? ? return CloseHandle( handle ); ?
? ? ? ? } ?
? ? } ?
} ?
========
.NET Framework自帶的文件內(nèi)存映射類
http://www.cnblogs.com/briny/archive/2012/11/25/2787188.html最近一直為文件內(nèi)存映射發(fā)愁,整個(gè)兩周一直折騰這個(gè)東西。在64位系統(tǒng)和32位系統(tǒng)還要針對內(nèi)存的高低位進(jìn)行計(jì)算。好麻煩。。還是沒搞定
偶然從MSDN上發(fā)現(xiàn).NET 4.0把內(nèi)存文件映射加到了.NET類庫中。。好像方便了很多啊。。比用C#直接調(diào)用WINDOWS API方便多了。所以
這個(gè)必須果斷記錄之。。。項(xiàng)目馬上要用,為了加強(qiáng)內(nèi)存數(shù)據(jù)交換的效率。。這個(gè)。。。必須啊。。
任務(wù)
使用的方法或?qū)傩?
從磁盤上的文件中獲取表示持久內(nèi)存映射文件的 MemoryMappedFile 對象。
MemoryMappedFile.CreateFromFile 方法。
獲取表示非持久內(nèi)存映射文件(與磁盤上的文件不關(guān)聯(lián))的 MemoryMappedFile 對象。
MemoryMappedFile.CreateNew 方法。
- 或 -
MemoryMappedFile.CreateOrOpen 方法。
獲取現(xiàn)有內(nèi)存映射文件(持久文件或非持久文件)的 MemoryMappedFile 對象。
MemoryMappedFile.OpenExisting 方法。
獲取針對內(nèi)存映射文件的順序訪問視圖的 UnmanagedMemoryStream 對象。
MemoryMappedFile.CreateViewStream 方法。
獲取針對內(nèi)存映射文件的隨機(jī)訪問視圖的 UnmanagedMemoryAccessor 對象。
MemoryMappedFile.CreateViewAccessor 方法。
獲取要用于非托管代碼的 SafeMemoryMappedViewHandle 對象。
MemoryMappedFile.SafeMemoryMappedFileHandle 屬性。
- 或 -
MemoryMappedViewAccessor.SafeMemoryMappedViewHandle 屬性。
- 或 -
MemoryMappedViewStream.SafeMemoryMappedViewHandle 屬性。
將內(nèi)存分配推遲到創(chuàng)建視圖后進(jìn)行(僅限于非持久文件)。
(若要確定當(dāng)前系統(tǒng)頁大小,請使用 Environment.SystemPageSize 屬性。)
帶 MemoryMappedFileOptions.DelayAllocatePages 值的 CreateNew 方法。
- 或 -
將 MemoryMappedFileOptions 枚舉作為參數(shù)的 CreateOrOpen 方法。
?
持久文件內(nèi)存映射:
CreateFromFile 方法基于磁盤上的現(xiàn)有文件創(chuàng)建一個(gè)內(nèi)存映射文件。
?1 using System;
?2 using System.IO;
?3 using System.IO.MemoryMappedFiles;
?4 using System.Runtime.InteropServices;
?5?
?6 class Program
?7 {
?8 ? ? static void Main(string[] args)
?9 ? ? {
10 ? ? ? ? long offset = 0x10000000; // 256 megabytes
11 ? ? ? ? long length = 0x20000000; // 512 megabytes
12?
13 ? ? ? ? // Create the memory-mapped file.
14 ? ? ? ? using (var mmf = MemoryMappedFile.CreateFromFile(@"c:\ExtremelyLargeImage.data", FileMode.Open,"ImgA"))
15 ? ? ? ? {
16 ? ? ? ? ? ? // Create a random access view, from the 256th megabyte (the offset)
17 ? ? ? ? ? ? // to the 768th megabyte (the offset plus length).
18 ? ? ? ? ? ? using (var accessor = mmf.CreateViewAccessor(offset, length))
19 ? ? ? ? ? ? {
20 ? ? ? ? ? ? ? ? int colorSize = Marshal.SizeOf(typeof(MyColor));
21 ? ? ? ? ? ? ? ? MyColor color;
22?
23 ? ? ? ? ? ? ? ? // Make changes to the view.
24 ? ? ? ? ? ? ? ? for (long i = 0; i < length; i += colorSize)
25 ? ? ? ? ? ? ? ? {
26 ? ? ? ? ? ? ? ? ? ? accessor.Read(i, out color);
27 ? ? ? ? ? ? ? ? ? ? color.Brighten(10);
28 ? ? ? ? ? ? ? ? ? ? accessor.Write(i, ref color);
29 ? ? ? ? ? ? ? ? }
30 ? ? ? ? ? ? }
31 ? ? ? ? }
32 ? ? }
33 }
34?
35 public struct MyColor
36 {
37 ? ? public short Red;
38 ? ? public short Green;
39 ? ? public short Blue;
40 ? ? public short Alpha;
41?
42 ? ? // Make the view brigher.
43 ? ? public void Brighten(short value)
44 ? ? {
45 ? ? ? ? Red = (short)Math.Min(short.MaxValue, (int)Red + value);
46 ? ? ? ? Green = (short)Math.Min(short.MaxValue, (int)Green + value);
47 ? ? ? ? Blue = (short)Math.Min(short.MaxValue, (int)Blue + value);
48 ? ? ? ? Alpha = (short)Math.Min(short.MaxValue, (int)Alpha + value);
49 ? ? }
50 }
?非持久文件內(nèi)存映射:
CreateNew 和 CreateOrOpen 方法創(chuàng)建一個(gè)未映射到磁盤上的現(xiàn)有文件的內(nèi)存映射文件。
?1 using System;
?2 using System.IO;
?3 using System.IO.MemoryMappedFiles;
?4 using System.Threading;
?5?
?6 class Program
?7 {
?8 ? ? // Process A:
?9 ? ? static void Main(string[] args)
10 ? ? {
11 ? ? ? ? using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("testmap", 10000))
12 ? ? ? ? {
13 ? ? ? ? ? ? bool mutexCreated;
14 ? ? ? ? ? ? Mutex mutex = new Mutex(true, "testmapmutex", out mutexCreated);
15 ? ? ? ? ? ? using (MemoryMappedViewStream stream = mmf.CreateViewStream())
16 ? ? ? ? ? ? {
17 ? ? ? ? ? ? ? ? BinaryWriter writer = new BinaryWriter(stream);
18 ? ? ? ? ? ? ? ? writer.Write(1);
19 ? ? ? ? ? ? }
20 ? ? ? ? ? ? mutex.ReleaseMutex();
21?
22 ? ? ? ? ? ? Console.WriteLine("Start Process B and press ENTER to continue.");
23 ? ? ? ? ? ? Console.ReadLine();
24?
25 ? ? ? ? ? ? Console.WriteLine("Start Process C and press ENTER to continue.");
26 ? ? ? ? ? ? Console.ReadLine();
27?
28 ? ? ? ? ? ? mutex.WaitOne();
29 ? ? ? ? ? ? using (MemoryMappedViewStream stream = mmf.CreateViewStream())
30 ? ? ? ? ? ? {
31 ? ? ? ? ? ? ? ? BinaryReader reader = new BinaryReader(stream);
32 ? ? ? ? ? ? ? ? Console.WriteLine("Process A says: {0}", reader.ReadBoolean());
33 ? ? ? ? ? ? ? ? Console.WriteLine("Process B says: {0}", reader.ReadBoolean());
34 ? ? ? ? ? ? ? ? Console.WriteLine("Process C says: {0}", reader.ReadBoolean());
35 ? ? ? ? ? ? }
36 ? ? ? ? ? ? mutex.ReleaseMutex();
37 ? ? ? ? }
38 ? ? }
39 }
========
總結(jié)
以上是生活随笔為你收集整理的C#内存映射文件学习总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自己写编译器学习总结
- 下一篇: C++内联函数学习总结