linux 进程 堆大小,Linux进程的默认分配堆大小

在Linux中的进程启动过程中,默认的allocated堆大小是多少?这与ulimit无关,而是因为注意到了question。

我还通过g++ -O0 -Wall -std=c++11和strace进行了以下测试,因此没有显示堆分配。

#include

using namespace std;

class C {

public:

int i;

};

int main() {

cout << "possible heap allocation below:" << endl;

auto c = new C;

auto i = c->i;

delete c;

cout << "Was anything revealed above?" << endl;

cout << "i = " << i << endl;

}

@ EDIT

跟随@Vaughn Cato的注释,它非常精确地揭示了一些预先分配的空间以及由于new, delete对象而引起的空间变化:

#include "malloc.h"

#include

class C {

public:

int i;

};

void prnt(struct mallinfo info) {

printf("Non-mmapped space allocated (bytes) : %d\n", info.arena);

printf("Number of free chunks : %d\n", info.ordblks);

printf("Number of free fastbin blocks : %d\n", info.smblks);

printf("Number of mmapped regions : %d\n", info.hblks);

printf("Space allocated in mmapped regions (bytes): %d\n", info.hblkhd);

printf("Maximum total allocated space (bytes) : %d\n", info.usmblks);

printf("Space in freed fastbin blocks (bytes) : %d\n", info.fsmblks);

printf("Total allocated space (bytes) : %d\n", info.uordblks);

printf("Total free space (bytes) : %d\n", info.fordblks);

printf("Top-most, releasable space (bytes) : %d\n", info.keepcost);

}

int main() {

struct mallinfo before_ctor = mallinfo();

auto c = new C;

struct mallinfo after_ctor = mallinfo();

auto i = c->i;

delete c;

struct mallinfo after_dtor = mallinfo();

printf("\n--- memory pre-allocated? -------------------- \n\n");

prnt(before_ctor);

printf("\n--- memory changed after \"new\" object? ----- \n\n");

prnt(after_ctor);

printf("\n--- memory changed after \"delete\" object? --- \n\n");

prnt(after_dtor);

printf("\ni = %d\n", i);

}

1

投票

没有默认堆大小。堆始终是动态的,并且从零开始。使用的系统调用为mmap,brk和sbrk。

大多数动态链接程序在程序加载器中使用堆。在为std::cout,FILE *stdout等设置输出缓冲区时,他们也使用它。>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值