[英]default allocated heap size for a Linux process
Linux 中进程启动期间的默认allocated
堆大小是多少? 这不是关于ulimit而是因为注意到这个问题。
我还通过g++ -O0 -Wall -std=c++11
和strace
进行了以下测试,没有用于更改new, delete
了测试的 object。
#include <iostream>
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;
}
@编辑
正如所建议的,下面它非常准确地显示了由于libc
运行时预分配的一些空间以及由于new, delete
通过 ltrace 看到的ltrace
:
#include "malloc.h"
#include <cstdio>
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);
}
没有默认的堆大小。 堆总是动态的并且从零开始。 使用的系统调用是mmap
、 brk
和sbrk
。
大多数动态链接程序在程序加载器中使用堆。 他们还在为std::cout
、 FILE *stdout
等设置 output 缓冲区时使用它。这就是您所看到的“初始堆”。
如果您在不使用 C 运行时支持库的情况下构建程序,您将看不到任何堆使用情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.