C++ 本课程中的居民记忆行为?
我正在处理驻留内存增加问题。 为了模拟这一点,我编写了一个代码片段,它实际上是在模拟我的问题C++ 本课程中的居民记忆行为?,c++,memory-management,memory-leaks,operating-system,C++,Memory Management,Memory Leaks,Operating System,我正在处理驻留内存增加问题。 为了模拟这一点,我编写了一个代码片段,它实际上是在模拟我的问题 #include <stdlib.h> #include <stdio.h> #include <string> #include <malloc.h> using namespace std; int main() { int count = 0; char szCmd[128]; system("> result_t
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <malloc.h>
using namespace std;
int main()
{
int count = 0;
char szCmd[128];
system("> result_top.txt");
while (count < 10)
{
sprintf(szCmd, "echo \"\nRound %d\n\" >> result_top.txt", count);
system(szCmd);
system("top -b -n 1 -p `pgrep a.out` | tail -4 >> result_top.txt");
int **t = new int*[100000];
for(long i = 0; i < 100000; i++)
{
t[i] = new int();
}
system("top -b -n 1 -p `pgrep a.out`| tail -4 >> result_top.txt");
for(long i = 100000 - 1; i >= 0; i--)
{
delete t[i];
}
delete [] t;
sleep(5);
system("top -b -n 1 -p `pgrep a.out`| tail -4 >> result_top.txt");
printf("round %d finished\n", count);
count++;
}
return 0;
}
现在在第0轮中,当我释放内存时,驻留内存是4028,在分配内存之前的第二次迭代中,它变成4048
增加了20个字节
在第一轮之后,每一轮都进展顺利。那么,增加20字节的原因是什么呢
类似的情况也发生在我的项目代码中。我不知道为什么会出现这种效果,但是请注意,操作系统不负责实现新的/删除功能。操作系统提供的唯一帮助是更改分配给进程的内存的总大小(通常在Unix系统上通过brk()/sbrk()请求) 因此,您看到的明显泄漏与new/delete的具体实现有关。在第二次delete调用满足某个条件之前,这些内存可能不会“垃圾收集”未使用的内存。在他们达到这一点并请求减少分配给进程的内存总大小之前,您无法观察到内存现在没有top之类的工具 在某种程度上,这是一个内存泄漏,因为空闲内存没有尽快返回到操作系统。但是,您可能相信new/delete实现应该以合理的方式释放内存,从而避免过度浪费内存
C++中,如果默认值不符合您的需要,可以更改NeX/DELL的实现。
< P>我自己取了这个代码并执行。 我想,在第0轮之后,您看到的20字节的增加是因为代码中尚未遇到的部分。因此,它尚未映射到驻留内存。 现在,当遇到将映射到驻留内存的新代码时 因此,您看到的20字节将增加要确定这一点,您可以删除上次top录制后新遇到的代码,并检查驻留内存,即20k不会有记录器。我建议使用外部工具查看进程的内存消耗情况。在顶部测量之间,例如,您调用printf/sprintf/system,谁知道他们在内部做什么。还要记住,在malloc实现决定这样做之前,RES可能不会降低。仅仅调用delete()并不意味着RES会下降。对于工具,我建议使用valgrind的massif或类似的东西:干杯
Round 0
/* Before Allocation RES */
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11053 root 20 0 11736 852 724 S 0.0 0.0 0:00.00 a.out
/* After allocation RES */
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11053 root 20 0 15688 4808 768 S 0.0 0.2 0:00.01 a.out
/* After Deletion RES*/
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11053 root 20 0 14904 4028 772 S 0.0 0.2 0:00.02 a.out
Round 1
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11053 root 20 0 14908 4048 788 S 0.0 0.2 0:00.02 a.out
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11053 root 20 0 15700 4828 788 S 0.0 0.2 0:00.05 a.out
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11053 root 20 0 11872 1052 788 S 0.0 0.0 0:00.06 a.out
Round 2
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11053 root 20 0 11872 1052 788 S 0.0 0.0 0:00.06 a.out
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11053 root 20 0 15688 4828 788 S 0.0 0.2 0:00.08 a.out
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11053 root 20 0 11872 1052 788 S 0.0 0.0 0:00.09 a.out
Round 3
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11053 root 20 0 11872 1052 788 S 0.0 0.0 0:00.09 a.out
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11053 root 20 0 15688 4828 788 S 0.0 0.2 0:00.11 a.out
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11053 root 20 0 11872 1052 788 S 0.0 0.0 0:00.13 a.out