Windows memory management study note(2)

本文介绍了Windows操作系统中虚拟内存、内存映射文件及堆这三种内存管理机制,并详细讲解了使用VirtualAlloc和VirtualFree函数来分配和释放虚拟内存的方法。
虚拟内存使用之前需要了解系统内存管理信息和虚拟地址空间的相关信息。
1.管理系统信息 —— 页面大小, 分配粒度 等基本的系统信息,CPU管理的信息。
使用函数: GetSystemInfo(LPSYSTEM_INFO psi)

2.管理内存的状态信息 —— 物理内存大小, 页交换文件的大小 等内存相关的信息
使用函数: lobalMemoryStatus(LPMEMORYSTATUS lpBuffer)

3.管理地址空间的状态 ——查询是否给某个地址调拨物理存储器,是否可以读取某一个内存地址
使用函数: VirtualQuery(LPCVOID pvAddress, PMEMORY_BASIC_INFORMATION pmbi, DWORD dwLength);


[b]Windows 操作内存的三种机制[/b]
虚拟内存 —— 用于管理大型对象数组或者大型结构数组
内存映射文件 —— 适合管理大型数据流,以及在同一台机器上运行多个进程之间的共享数据。
堆 —— 适合管理大量的小型对象

[b]虚拟内存的操作方法[/b]
一,分配虚拟内存
PVOID VritualAlloc(PVOID pvAddress, SIZE_T dwSize, DWORD fdwAllocationType, DWORD fdwProtect)

参数说明
pvAddress : 需要预定的基地址,如果由系统自己决定可以为null, 如果给定值那么需要时分配粒度的整数陪。系统可以自动调整

dwSize: 需要预定的地址空间的大小。该值表示了具体需要预定几页。

fdwAllocationType : 该属性指定了如何分配内存,可以是预定地址空间区域,也可以是调拨物理存储交换页文件,另外还可以是设定可重置页。

fdwProtect: 给定区域的保护属性。

1) 预定地址空间区域 —— 分配虚拟地址空间,将需要的页表信息加载,但是不分配具体的页交换文件
其中fdwAllocationType = MEM_RESERVE

2) 给预定的地址空间区域调拨存储器 —— 这个函数才真正的从物理存储器的也交换文件调拨给指定的区域。可以预定一大块地址空间,但是只调拨其中的个别页,进行处理从而提高内存的利用率。
其中fdwAllocationType = MEM_COMMIT

3) 同时预定和调拨物理存储器 —— 先预定地址空间区域,然后马上给预定的区域调拨物理存储器
其中fdwAllocationType = MEM_COMMIT | MEM_RESERVE

4) 重置物理存储器的内容 —— 程序通过指定物理存储器没有被修改,从而减少系统页交换的操作时间。
其中fdwAllocationType = MEM_RESET

函数同预定地址空间的函数,关键是参数 fdwAllocationType 必须为MEM_COMMIT

二 释放虚拟内存
BOOL VirtualFree( LPVOID pvAddress, SIZE_T dwSize, DWORD fdwFreeType)
参数说明
pvAddress : 需要撤销的也交换文件的基地址
dwSize: 需要释放的物理存储器的大小
fdwAllocationType : 指定撤销的方式,主要是只撤销部分物理存储器也交换文件还是撤销整个地址空间区域。

1)撤销调拨物理存储器也交换文件 —— 仅仅释放调拨给地址空间区域的一部分存储器
pvAddress : 需要指定撤销物理存储器的基地址。
dwSize: 需要释放的物理存储器的大小
fdwAllocationType : MEM_DECOMMIT

2) 撤销整个物理存储交换文件和预定的地址空间区域
pvAddress : 由预定地址空间返回的基地址
wSize: 0 系统指导区域的大小
fdwAllocationType : MEM_RELEASE
### Memory Management in Computer Systems Memory management in computer systems is a critical aspect of operating system design and system architecture, ensuring efficient allocation and utilization of memory resources. It involves managing both physical and virtual memory to optimize system performance and resource utilization. In traditional paged memory management systems, each process runs in its own isolated address space. This isolation helps prevent processes from interfering with each other, enhancing system stability and security. However, some operating systems adopt a single address space model, where all processes share a globally unified address space. Examples of such systems include IBM i, which allows all processes to operate within a large, shared address space, and IBM OS/VS2/SVS, which constrained all jobs to a single 16 MiB virtual address space [^3]. The choice of memory layout also plays a significant role in memory management efficiency. In-memory computing systems often utilize different data layouts such as row-major, column-major, and compressed formats. Row-major layouts are beneficial for accessing contiguous elements in a row, while column-major formats are more suitable for vectorized operations. Compressed formats like bit-packed arrays or dictionary encoding can significantly reduce memory footprint and improve cache performance [^1]. Emerging memory technologies, such as non-volatile memory (NVM), offer the speed characteristics of DRAM with the persistence of SSDs. These technologies introduce new challenges and opportunities in memory management, as they blur the traditional boundaries between volatile and persistent storage [^2]. Modern database systems, which heavily rely on in-memory operations, are particularly sensitive to memory management strategies. Efficient memory management is essential for optimizing query performance, reducing latency, and improving overall system throughput. Despite careful design, state-of-the-art database management systems (DBMS) can still become performance bottlenecks in many applications [^4]. ### Example: Memory Layout in In-Memory Databases ```python # Example of row-major and column-major layout in a 2D array import numpy as np # Row-major layout (C-style) row_major = np.array([[1, 2, 3], [4, 5, 6]], order='C') print("Row-major layout:") print(row_major) # Column-major layout (Fortran-style) column_major = np.array([[1, 2, 3], [4, 5, 6]], order='F') print("\nColumn-major layout:") print(column_major) ``` This code demonstrates how NumPy can be used to create arrays with different memory layouts, which can have significant implications for performance depending on the access patterns of the application. ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值