c++ - 在 Solaris 上,连续内存分配不会减小镜像大小

标签 c++ linux memory-management solaris-10

我在 Solaris sparc 服务器上编写了一个测试程序,并使用 Sun Studio 对其进行了编译

#include <iostream>

using namespace std;

int main()
{
    const int size =  9999;
    char *ptr[size];

    while(1)
    {
       for(int i = 0; i < size; i++)
       {
           ptr[i] = new char[2048];
       }
        for(int i = 0; i < size; i++)
        {
           delete[] ptr[i];
        }
    }
    return  9;
}

compiled it as
CC -m64 -g 

现在,当我运行它时,我可以看到进程大小不断增加,并且当达到系统内存限制时进程崩溃。我使用 truss 追踪它,只能看到 brk 系统调用。 在搜索一些 Oracle 站点时,我设置了 LD_PRELOAD=libmapmalloc.so,然后进程大小是恒定的。 truss 显示这次它使用 malloc 来映射匿名内存页。

另一方面,我也尝试在 RHEL Linux 2.6 x86 机器上看到这种行为,它也使用了 truss,但进程大小是恒定的。

我不明白 Solaris 在第一种情况下的行为,它使用 brk 来增加数据段的大小,但当我执行删除时,它并没有减少它。有人可以解释一下为什么 Solaris 这样做吗?

Linux 在这里做了什么不同的事情来保持进程大小恒定,因为它也使用相同的系统调用。

谢谢 尼拉吉·拉蒂

最佳答案

让我引用一下official oracle docs :

Note that after free() is executed, the freed space is made available for further allocation by the application and not returned to the system. Memory is returned to the system only when the application terminates.

关于c++ - 在 Solaris 上,连续内存分配不会减小镜像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18134405/

相关文章:

c++ - 运行使用 openCV 1.0 的应用程序时出现问题

C++ 模板 : no matching function for call

c++ - 如何在第 32 周的 Guru 中实现 DERR_ENTRY 宏?

android - cmake - 一次针对多个架构,无需在构建之间手动清理

iphone - 仍然对 Objective C 中的内存管理感到好奇

c - 虚拟页面权限位和区域结构权限位之间的区别

linux - 使用 bash 在连字符之间批量重命名文件名

jquery - 为什么 jQuery 显然已停止在 Firefox 22 中工作?

ios - 内存问题 - 生存与整体 -> 应用程序被杀死

java - 在 crate.yml 中启动具有特定 HEAP 数量的节点