<tfoot id='DfKKv'></tfoot>

      <bdo id='DfKKv'></bdo><ul id='DfKKv'></ul>

      <small id='DfKKv'></small><noframes id='DfKKv'>

    1. <legend id='DfKKv'><style id='DfKKv'><dir id='DfKKv'><q id='DfKKv'></q></dir></style></legend>
      <i id='DfKKv'><tr id='DfKKv'><dt id='DfKKv'><q id='DfKKv'><span id='DfKKv'><b id='DfKKv'><form id='DfKKv'><ins id='DfKKv'></ins><ul id='DfKKv'></ul><sub id='DfKKv'></sub></form><legend id='DfKKv'></legend><bdo id='DfKKv'><pre id='DfKKv'><center id='DfKKv'></center></pre></bdo></b><th id='DfKKv'></th></span></q></dt></tr></i><div id='DfKKv'><tfoot id='DfKKv'></tfoot><dl id='DfKKv'><fieldset id='DfKKv'></fieldset></dl></div>

    2. C++ 删除不会释放所有内存(Windows)

      时间:2023-10-06
      <legend id='GUP6q'><style id='GUP6q'><dir id='GUP6q'><q id='GUP6q'></q></dir></style></legend>

        <i id='GUP6q'><tr id='GUP6q'><dt id='GUP6q'><q id='GUP6q'><span id='GUP6q'><b id='GUP6q'><form id='GUP6q'><ins id='GUP6q'></ins><ul id='GUP6q'></ul><sub id='GUP6q'></sub></form><legend id='GUP6q'></legend><bdo id='GUP6q'><pre id='GUP6q'><center id='GUP6q'></center></pre></bdo></b><th id='GUP6q'></th></span></q></dt></tr></i><div id='GUP6q'><tfoot id='GUP6q'></tfoot><dl id='GUP6q'><fieldset id='GUP6q'></fieldset></dl></div>

          <small id='GUP6q'></small><noframes id='GUP6q'>

            <tbody id='GUP6q'></tbody>
            • <bdo id='GUP6q'></bdo><ul id='GUP6q'></ul>
                <tfoot id='GUP6q'></tfoot>

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

                上一篇:如何将 unordered_set 与自定义类型一起使用? 下一篇:C4250 VC++ 警告是什么意思?

                相关文章

                最新文章

                <legend id='1dwJZ'><style id='1dwJZ'><dir id='1dwJZ'><q id='1dwJZ'></q></dir></style></legend>

                1. <tfoot id='1dwJZ'></tfoot>

                  <small id='1dwJZ'></small><noframes id='1dwJZ'>

                2. <i id='1dwJZ'><tr id='1dwJZ'><dt id='1dwJZ'><q id='1dwJZ'><span id='1dwJZ'><b id='1dwJZ'><form id='1dwJZ'><ins id='1dwJZ'></ins><ul id='1dwJZ'></ul><sub id='1dwJZ'></sub></form><legend id='1dwJZ'></legend><bdo id='1dwJZ'><pre id='1dwJZ'><center id='1dwJZ'></center></pre></bdo></b><th id='1dwJZ'></th></span></q></dt></tr></i><div id='1dwJZ'><tfoot id='1dwJZ'></tfoot><dl id='1dwJZ'><fieldset id='1dwJZ'></fieldset></dl></div>
                    <bdo id='1dwJZ'></bdo><ul id='1dwJZ'></ul>