本文介绍了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).

问题是:我动态分配一个内存2立即释放它。仍然,在内存分配之前,我的进程内存使用量在分配前为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 ++中的代码我' m使用:

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;
}


推荐答案

重复,但我会回答它:

如果你正在查看任务管理器大小,它会给你的过程的大小。如果没有压力(你的系统有足够的内存可用,没有进程被饥饿),减少进程的虚拟内存使用是没有意义的 - 一个进程的增长,收缩,增长,在循环模式中收缩,当它处理数据时分配,然后释放在一个处理周期中使用的数据,为下一个周期分配存储器,然后再次释放它。如果操作系统要重新获得那些页面的内存,只需要再次将它们返回到您的进程,这将是浪费处理能力(分配和取消分配页面到一个特定的进程是不是微不足道,特别是如果你不能确定那些页面属于谁,因为他们需要被清理[用零或一些其他常量填充,以确保新所有者不能使用内存为钓鱼旧数据,例如找到存储在存储器中的密码])。

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 的内存中存储此内存已被删除 $ c>。这是为了帮助确定免费使用。因此,如果您的应用程序正在调试模式下运行,则不要期望释放任何释放的内存。它会被重复使用。因此,如果你运行你的代码三次,它不会增长到三倍的大小。

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 19:58
查看更多