windows内存管理

一 windows系统的内存管理涉及哪些

1.1 虚拟内存管理机制

  • windows操作系统使用虚拟内存技术,将磁盘文件,通过映射对象(存储在物理内存)关联,映射到虚拟内存作为文件试图。即用户操作"虚拟内存中File View Object"-> 物理内存中"File Mapping" -> 磁盘文件。

1.2 分页和分段机制

  • windows采用分页机制,将内存划分为固定大写的页面,通过页表来映射虚拟地址和物理地址
  • 分段机制是将内存划分为不同大小的段,通过段表来管理。

1.3 内存保护

windows通过硬件机制保护系统内存资源,防止程序对内存的非法访问,确保系统的稳定性和安全性

1.4 高级内存管理

windows还提供内存压缩、内存回收、内存优化等功能
内存压缩可以节省生存空间,内存回收能释放不再使用的内存资源,而内存优化则旨在提高系统性能和响应速度。

二 虚拟内存的实现技术 - 文件映射

2.1 磁盘文件,文件映射对象,文件视图的关系

MSDN - 文件映射
在这里插入图片描述
注:
文件映射: 将文件内存(File on Disk)于进程的一部分虚拟地址空间关联。
文件映射对象:调用CreateFileMapping创建文件映射对象,维护磁盘文件和虚拟地址关联。
文件视图: 进程用来访问磁盘文件的虚拟地址空间部分。如果使用指针从文件视图读取和写入文件视图的过程,就像使用动态分配的内存一样。使用文件映射可提高可提高效率,因为文件驻留在磁盘上,但文件视图驻留在内存中。

2.2 应用场景(为什么要用文件映射?)

1)解决大文件频繁读写效率问题
直接加载文件到内存(传统方式)
C++读取txt文件

1 逐行读取
void readTxt(string file)
{ifstream infile; infile.open(file.data());   //将文件流对象与文件连接起来 assert(infile.is_open());   //若失败,则输出错误消息,并终止程序运行 string s;while(getline(infile,s)){cout<<s<<endl;}infile.close();             //关闭文件输入流 
}

传统方式,将磁盘文件,加载到内存中,频繁操作磁盘文件。
文件映射方式,将磁盘文件,通过关联到文件映射对象中,读取IO效率更高。减少磁盘文件访问次数。
2) 解决多个进程访问共享磁盘文件场景
多个进程,都可以访问文件映射对象。解决IPC通信,数据复制的开销。

2.3 原理

通过文件映射机制,进程可以通过访问内存的方式直接读写磁盘文件,修改内容后由操作系统自动同步的文件进行更新。使用FlushViewOfFile可刷新缓存区,将映射在虚拟内存当中的数据立即回写到磁盘文件。
过程如下所示:
1. CreateFileMapping创建文件映射对象(内核对象,多进程可访问),关联磁盘文件(文件句柄hFile)
2. MapViewOfFile将文件映射对象,映射到进程虚拟地址

2.4 代码

/*This program demonstrates file mapping, especially how to align aview with the system file allocation granularity.
*/#include <windows.h>
#include <stdio.h>
#include <tchar.h>#define BUFFSIZE 1024 // size of the memory to examine at any one time#define FILE_MAP_START 138240 // starting point within the file of// the data to examine (135K)/* The test file. The code below creates the file and populates it,so there is no need to supply it in advance. */TCHAR * lpcTheFile = TEXT("fmtest.txt"); // the file to be manipulatedint main(void)
{HANDLE hMapFile;      // handle for the file's memory-mapped regionHANDLE hFile;         // the file handleBOOL bFlag;           // a result holderDWORD dBytesWritten;  // number of bytes writtenDWORD dwFileSize;     // temporary storage for file sizesDWORD dwFileMapSize;  // size of the file mappingDWORD dwMapViewSize;  // the size of the viewDWORD dwFileMapStart; // where to start the file map viewDWORD dwSysGran;      // system allocation granularitySYSTEM_INFO SysInfo;  // system information; used to get granularityLPVOID lpMapAddress;  // pointer to the base address of the// memory-mapped regionchar * pData;         // pointer to the dataint i;                // loop counterint iData;            // on success contains the first int of dataint iViewDelta;       // the offset into the view where the data//shows up// Create the test file. Open it "Create Always" to overwrite any// existing file. The data is re-created belowhFile = CreateFile(lpcTheFile,GENERIC_READ | GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);if (hFile == INVALID_HANDLE_VALUE){_tprintf(TEXT("hFile is NULL\n"));_tprintf(TEXT("Target file is %s\n"),lpcTheFile);return 4;}// Get the system allocation granularity.GetSystemInfo(&SysInfo);dwSysGran = SysInfo.dwAllocationGranularity;// Now calculate a few variables. Calculate the file offsets as// 64-bit values, and then get the low-order 32 bits for the// function calls.// To calculate where to start the file mapping, round down the// offset of the data into the file to the nearest multiple of the// system allocation granularity.dwFileMapStart = (FILE_MAP_START / dwSysGran) * dwSysGran;_tprintf (TEXT("The file map view starts at %ld bytes into the file.\n"),dwFileMapStart);// Calculate the size of the file mapping view.dwMapViewSize = (FILE_MAP_START % dwSysGran) + BUFFSIZE;_tprintf (TEXT("The file map view is %ld bytes large.\n"),dwMapViewSize);// How large will the file mapping object be?dwFileMapSize = FILE_MAP_START + BUFFSIZE;_tprintf (TEXT("The file mapping object is %ld bytes large.\n"),dwFileMapSize);// The data of interest isn't at the beginning of the// view, so determine how far into the view to set the pointer.iViewDelta = FILE_MAP_START - dwFileMapStart;_tprintf (TEXT("The data is %d bytes into the view.\n"),iViewDelta);// Now write a file with data suitable for experimentation. This// provides unique int (4-byte) offsets in the file for easy visual// inspection. Note that this code does not check for storage// medium overflow or other errors, which production code should// do. Because an int is 4 bytes, the value at the pointer to the// data should be one quarter of the desired offset into the filefor (i=0; i<(int)dwSysGran; i++){WriteFile (hFile, &i, sizeof (i), &dBytesWritten, NULL);}// Verify that the correct file size was written.dwFileSize = GetFileSize(hFile,  NULL);_tprintf(TEXT("hFile size: %10d\n"), dwFileSize);// Create a file mapping object for the file// Note that it is a good idea to ensure the file size is not zerohMapFile = CreateFileMapping( hFile,          // current file handleNULL,           // default securityPAGE_READWRITE, // read/write permission0,              // size of mapping object, highdwFileMapSize,  // size of mapping object, lowNULL);          // name of mapping objectif (hMapFile == NULL){_tprintf(TEXT("hMapFile is NULL: last error: %d\n"), GetLastError() );return (2);}// Map the view and test the results.lpMapAddress = MapViewOfFile(hMapFile,            // handle to// mapping objectFILE_MAP_ALL_ACCESS, // read/write0,                   // high-order 32// bits of file// offsetdwFileMapStart,      // low-order 32// bits of file// offsetdwMapViewSize);      // number of bytes// to mapif (lpMapAddress == NULL){_tprintf(TEXT("lpMapAddress is NULL: last error: %d\n"), GetLastError());return 3;}// Calculate the pointer to the data.pData = (char *) lpMapAddress + iViewDelta;// Extract the data, an int. Cast the pointer pData from a "pointer// to char" to a "pointer to int" to get the whole thingiData = *(int *)pData;_tprintf (TEXT("The value at the pointer is %d,\nwhich %s one quarter of the desired file offset.\n"),iData,iData*4 == FILE_MAP_START ? TEXT("is") : TEXT("is not"));// Close the file mapping object and the open filebFlag = UnmapViewOfFile(lpMapAddress);bFlag = CloseHandle(hMapFile); // close the file mapping objectif(!bFlag){_tprintf(TEXT("\nError %ld occurred closing the mapping object!"),GetLastError());}bFlag = CloseHandle(hFile);   // close the file itselfif(!bFlag){_tprintf(TEXT("\nError %ld occurred closing the file!"),GetLastError());}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/842817.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring AOP:使用Spring AOP进行切面编程的实例,解释不同类型的advice(advice类型)以及何时使用它们

I. Spring AOP 简介 AOP的含义和用途: AOP,全称Aspect Oriented Programming,即面向切面编程,是一种编程范式,与面向对象编程(OOP)并列。AOP是一种处理程序中横切关注点的技术,这些横切关注点包括日志记录,安全控制,缓存,事务管理等。 AOP提供了在软件组件的行为…

C-数据结构-树状存储基本概念

‘’’ 树状存储基本概念 深度&#xff08;层数&#xff09; 度&#xff08;子树个数&#xff09; 叶子 孩子 兄弟 堂兄弟 二叉树&#xff1a; 满二叉树&#xff1a; 完全二叉树&#xff1a; 存储&#xff1a;顺序&#xff0c;链式 树的遍历&#xff1a;按层遍历&#xff0…

Kibana(一张图片胜过千万行日志)

Kibana&#xff08;一张图片胜过千万行日志&#xff09; Kibana是一个开源的分析和可视化平台&#xff0c;设计用于和Elasticsearch一起工作。 你用Kibana来搜索&#xff0c;查看&#xff0c;并和存储在Elasticsearch索引中的数据进行交互。 你可以轻松地执行高级数据分析&a…

LangChain 0.2 - 基于 SQL 数据构建问答系统

本文翻译整理自&#xff1a;Build a Question/Answering system over SQL data https://python.langchain.com/v0.2/docs/tutorials/sql_qa/ 文章目录 一、项目说明⚠️ 安全说明⚠️架构 二、设置三、Chains1、将问题转换为 SQL查询2、执行 SQL查询3、回答问题 四、Agents1、S…

TiDB学习3:TiKV

目录 1. TiKV架构和作用 2. RocksDB 2.1 写入 2.2 查询 2.3 Column Families列簇 3. 分布式事务 3.1 事务流程 3.2 分布式事务流程 3.3 MVCC 4. Raft与Multi Raft 4.1 Raft日志复制 4.2 Raft Leader选举 5. TiKV- 读写 5.1 数据的写入 5.2 数据的读取ReadIndex …

夏日防晒笔记

1 防晒霜 使用方法&#xff1a;使用前上下摇晃瓶身4至5次&#xff0c;在距离肌肤10至15cm处均匀喷上。如在面部使用&#xff0c;请先喷在掌心再均匀涂抹于面部。排汗量较多时或擦拭肌肤后&#xff0c;请重复涂抹以确保防晒效果。卸除时使用普通洁肤产品洗净即可。

leetcode-主持人调度(二)-110

题目要求 思路 1.先将开始时间和结束时间拆分放到两个数组中进行排序 2.如果开始的时间小于结束时间&#xff0c;说明目前没有空闲的人&#xff0c;需要增加人&#xff0c;如果大于等于&#xff0c;说明有人刚结束了主持&#xff0c;可以进行新的主持了&#xff0c;变更到下一…

winform中通过PictureBox控件显示图像的代码

using System; using System.Drawing; using System.Windows.Forms; namespace DisplayImageExample { public partial class Form1 : Form { private PictureBox pictureBox1 new PictureBox(); public Form1(){InitializeComponent();// 初始化PictureBox控件pictureBox1.S…

驱动开发的分离与分层

1.概念 1.1.分层 分层就是将一个复杂的工作分成了多层, 分而做之,降低难度。在驱动里面&#xff0c;每一层只专注于自己的事情, 系统已经将其中的核心层和事件处理层写好了&#xff0c;我们只需要来写硬件相关的驱动层代码即可。可能驱动里面大家都不是很熟悉&#xff0c;比如…

ubuntu18.04 报错:fatal error: execution

一、问题描述 在ubuntu18.04上编译Faster-lio 报错&#xff1a; fatal error: execution: 没有那个文件或目录#include <execution> 二、解决方法 需要将g编译器更新到9.0 sudo add-apt-repository ppa:ubuntu-toolchain-r/test sudo apt update sudo apt install g…

深度学习-序列模型

深度学习-序列模型 1. 定义2. 应用领域3. 典型模型4. 技术细节5. 总结 序列模型是一种处理序列数据的机器学习模型&#xff0c;其输入和/或输出通常为序列形式的数据。以下是关于序列模型的详细解释&#xff1a; 1. 定义 序列模型是输入输出均为序列数据的模型&#xff0c;它…

【Django】开发个人博客系统【1】

使用Django开发个人博客系统&#xff0c;博客系统包括用户&#xff08;博主&#xff09;注册和登录、博主资料信息、图片墙功能、留言板功能、文章列表、文章正文内容和Admin后台系统。 1. 项目架构设计 下一步将上述设置写入Django的配置文件settings.py&#xff0c;当Django…

【区分vue2和vue3下的elementUI和elementUI Plus的container组件,介绍如何安装,属性,事件,方法等以及使用案例】

vue2下的Element UI与vue3下的Element Plus的container组件区分 一、Element UI (vue2) 1. 安装 使用npm进行安装&#xff1a; npm install element-ui --save2. Container组件介绍 Container组件用于布局的容器组件&#xff0c;方便快速搭建页面的基本结构。它包含了<…

R可视化:另类的柱状图

介绍 方格状态的柱状图 加载R包 knitr::opts_chunk$set(echo TRUE, message FALSE, warning FALSE) library(patternplot) library(png) library(ggplot2) library(gridExtra)rm(list ls()) options(stringsAsFactors F)导入数据 data <- read.csv(system.file(&qu…

【代码随想录——回溯算法——三周目】

1. 子集2 这题需要先进行排序&#xff0c;和候选人那题类似。防止出现重复的子集。 func subsetsWithDup(nums []int) [][]int {path : make([]int, 0)res : make([][]int, 0)sort.Ints(nums)var dfs func(nums []int, start int)dfs func(nums []int, start int) {res app…

保留两位小数不四舍五入,10000.55变成10000.54的坑

正解 function moneyFormat(num){ let money num "";//隐式转换为字符串和toString()效果一样//没有小数补齐这个0if(money.indexOf(".")"-1"){moneymoney".00";}else{//有小数截取前二位小数moneymoney.substring(0,money.inde…

多线程基本常识

多线程的状态 在Java中&#xff0c;一个线程的生命周期有以下几种状态&#xff1a; 新建&#xff08;New&#xff09;&#xff1a;当线程对象被创建时&#xff0c;线程处于新建状态。此时线程对象存在&#xff0c;但还没有调用start()方法启动线程。 运行&#xff08;Runnable…

逆向基础:软件手动脱壳技术入门

这里整合了一下之前自己学习软件手工脱壳的一些笔记和脱文&#xff0c;希望能给新学软件逆向和脱壳的童鞋们一点帮助。 1 一些概念 1.1 加壳 加壳的全称应该是可执行程序资源压缩&#xff0c;是保护文件的常用手段。加壳过的程序可以直接运行&#xff0c;但是不能查看源代码…

基于多源数据的微服务系统失败测试用例诊断

简介 本文介绍由南开大学、华为云及清华大学共同合作的论文:基于多源数据的微服务系统失败测试用例诊断。该论文已被FSE 2024&#xff08;The ACM International Conference on the Foundations of Software Engineering&#xff09; 会议录用&#xff0c;论文标题为: Fault D…

【MySQL】库的操作+表的操作

库的操作表的操作 1.库的操作1.1创建数据库1.2删除数据库1.3查找数据库1.4修改数据库1.5数据库备份和恢复1.6查看连接情况 2.库的操作2.1创建表2.2查看表结构2.3修改表2.4删除表 点赞&#x1f44d;&#x1f44d;收藏&#x1f31f;&#x1f31f;关注&#x1f496;&#x1f496; …