C++ delete does not free all memory (Windows)(C++ 删除不会释放所有内存(Windows))
问题描述
我需要帮助了解我在 Windows 上的内存分配和释放问题.我目前正在使用 VS11 编译器(VS2012 IDE)和最新更新(更新 3 RC).
I need help understanding problems with my memory allocation and deallocation on Windows. I'm using VS11 compiler (VS2012 IDE) with latest update at the moment (Update 3 RC).
问题是:我正在为二维数组动态分配一些内存并立即释放它.尽管如此,在内存分配之前,我的进程内存使用量在分配前为 0,3 MB,在分配时为 259,6 MB(预期因为分配了 32768 个 64 位整数(8 字节)数组),分配期间为 4106,8 MB,但在释放内存后并没有下降到预期的 0.3 MB,而是停留在 12.7 MB.由于我要释放占用的所有堆内存,因此我预计内存会回到 0.3 MB.
Problem is: I'm allocating dynamically some memory for a 2-dimensional array and immediately deallocating it. Still, before memory allocation, my process memory usage is 0,3 MB before allocation, on allocation 259,6 MB (expected since 32768 arrays of 64 bit ints (8bytes) are allocated), 4106,8 MB during allocation, but after deallocation memory does not drop to expected 0,3 MB, but is stuck at 12,7 MB. Since I'm deallocating all heap memory I've taken, I've expected memory to be back to 0,3 MB.
这是我使用的 C++ 代码:
This is the code in C++ I'm using:
#include <iostream>
#define SIZE 32768
int main( int argc, char* argv[] ) {
std::getchar();
int ** p_p_dynamic2d = new int*[SIZE];
for(int i=0; i<SIZE; i++){
p_p_dynamic2d[i] = new int[SIZE];
}
std::getchar();
for(int i=0; i<SIZE; i++){
for(int j=0; j<SIZE; j++){
p_p_dynamic2d[i][j] = j+i;
}
}
std::getchar();
for(int i=0; i<SIZE; i++) {
delete [] p_p_dynamic2d[i];
}
delete [] p_p_dynamic2d;
std::getchar();
return 0;
}
推荐答案
我确定这是重复的,但我还是会回答:
I'm sure this is a duplicate, but I'll answer it anyway:
如果您正在查看任务管理器的大小,它将为您提供进程的大小.如果没有压力"(你的系统有足够的可用内存,并且没有进程被饿死),那么减少进程的虚拟内存使用是没有意义的——进程增长、收缩、增长并不罕见,以循环模式收缩,因为它在处理数据时分配,然后释放在一个处理周期中使用的数据,为下一个周期分配内存,然后再次释放它.如果操作系统要重新获得"这些内存页面,只需要再次将它们返回给您的进程,那将浪费处理能力(将页面分配和取消分配给特定进程并非完全微不足道,特别是如果您首先无法确定这些页面属于谁,因为它们需要清理"[填充零或其他一些常量以确保新所有者"不能将内存用于钓鱼"旧数据",例如查找存储在内存中的密码]).
If you are viewing Task Manager size, it will give you the size of the process. If there is no "pressure" (your system has plenty of memory available, and no process is being starved), it makes no sense to reduce a process' virtual memory usage - it's not unusual for a process to grow, shrink, grow, shrink in a cyclical pattern as it allocates when it processes data and then releases the data used in one processing cycle, allocating memory for the next cycle, then freeing it again. If the OS were to "regain" those pages of memory, only to need to give them back to your process again, that would be a waste of processing power (assigning and unassigning pages to a particular process isn't entirely trivial, especially if you can't know for sure who those pages belonged to in the first place, since they need to be "cleaned" [filled with zero or some other constant to ensure the "new owner" can't use the memory for "fishing for old data", such as finding my password stored in the memory]).
即使页面仍然保留在该进程的所有权中,但未被使用,实际的 RAM 也可以被另一个进程使用.所以如果页面有一段时间没有发布也没什么大不了的.
Even if the pages are still remaining in the ownership of this process, but not being used, the actual RAM can be used by another process. So it's not a big deal if the pages haven't been released for some time.
此外,在调试模式下,C++ 运行时将在所有经过 delete
的内存中存储此内存已被删除".这是为了帮助识别免费使用".因此,如果您的应用程序在调试模式下运行,那么不要期望任何释放的内存会被释放.它将被重用.因此,如果您将代码运行三遍,它的大小不会增长到三倍.
Further, in debug mode, the C++ runtime will store "this memory has been deleted" in all memory that goes through delete
. This is to help identify "use after free". So, if your application is running in debug mode, then don't expect any freed memory to be released EVER. It will get reused tho'. So if you run your code three times over, it won't grow to three times the size.
这篇关于C++ 删除不会释放所有内存(Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 删除不会释放所有内存(Windows)
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01