在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等设置输出缓冲区时,他们也使用它。>