c++ - 增加 Virtual Bytes 的操作和函数

标签 c++ windows memory-management winapi virtual-memory

在 Windows 中遇到 32 位进程的一些内存不足问题时,我开始使用性能监视器来记录该进程的某些计数器。

虽然虚拟字节数高于私有(private)字节数和工作集是正常的,但我发现在我的情况下存在实质性差异,虚拟字节数高于私有(private)字节数和工作集设置。

哪些特定操作和 Win32/CRT 函数(在 C 或 C++ 中)会增加虚拟字节数但不会增加私有(private)字节数和工作集?

如果我理解 Performance Monitor 中不同计数器的描述,我想这将是某种共享资源。


由于在不同版本的 Windows 以及同一版本的 Windows 中的不同应用程序中使用内存计数器的命名约定似乎存在一些(至少可以说)混淆,我整理了以下内容:

来自MSDN的信息

根据 MSDN - Memory Limits for Windows Releases ,32 位 Windows 中每个 32 位进程的用户模式虚拟地址空间限制通常为 2 GB。使用 IMAGE_FILE_LARGE_ADDRESS_AWARE4G​​T 时最大可达 3 GB。

根据 MSDN - Memory Performance Information,下面是对性能监视器中不同计数器的描述,以及任务管理器中的相应列和保存信息的 Win32 结构。 .

虚拟字节

Virtual Bytes is the current size, in bytes, of the virtual address space the process is using. Use of virtual address space does not necessarily imply corresponding use of either disk or main memory pages. Virtual space is finite, and the process can limit its ability to load libraries.

任务管理器 XP:不适用
任务管理器 Vista:N/A
结构:MEMORYSTATUSEX.ullTotalVirtual-MEMORYSTATUSEX.ullAvailVirtual

私有(private)字节

Private Bytes is the current size, in bytes, of memory that this process has allocated that cannot be shared with other processes.

任务管理器 XP:VM 大小
任务管理器 Vista:提交大小
结构:PROCESS_MEMORY_COUNTERS_EX.PrivateUsage

工作集

Working Set is the current size, in bytes, of the Working Set of this process. The Working Set is the set of memory pages touched recently by the threads in the process. If free memory in the computer is above a threshold, pages are left in the Working Set of a process even if they are not in use. When free memory falls below a threshold, pages are trimmed from Working Sets. If they are needed they will then be soft-faulted back into the Working Set before leaving main memory.

任务管理器 XP:内存使用情况
任务管理器 Vista:工作集
结构:PROCESS_MEMORY_COUNTERS_EX.WorkingSetSize

最佳答案

我现在能想到的(可能)增加虚拟字节而不增加私有(private)字节的事情:

  • 二进制文件通常是共享的(即非私有(private)的),但会占用大量地址空间。这甚至可以大于二进制文件的大小

  • 使用 VirtualAlloc 保留顺序地址空间而不提交/访问它。自定义内存管理器可能会这样做。

  • 使用内存映射文件(不完全访问它)

关于c++ - 增加 Virtual Bytes 的操作和函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1581925/

相关文章:

用于信号处理和用户管理的 Python Unix/Windows 抽象层

java - 什么时候必须关闭数据库?

c++ - 没有删除运算符的 shared_ptr 内存泄漏

c++ - 为什么人们似乎暗示我宁愿不使用 Boost?

c++ - 我可以从 STA dll 运行 QT 或 wxWidgets GUI 吗?

c++ - 将 int 类型转换为 double C++

Windows 7 无线服务未按预期工作

c - 什么是不同的功能 : `malloc()` and `kmalloc()` ?

c++ - 给定相同数据结构的两个迭代器,如何检查哪个迭代器在另一个之前?

c++ - HTML5 桌面应用程序和 C++ 业务逻辑