sof
sof

Reputation: 9647

default allocated heap size for a Linux process

What is the default allocated heap size during a process startup in Linux? It's not about ulimit but because of noticing this question.

I also did the following test via g++ -O0 -Wall -std=c++11 and strace, no syscalls for the change of heap allocation on new, delete the tested object were revealed.

 #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;                                                                                                                                                                                                 
 }        

@EDIT

As suggested, below it reveals quite precisely some space pre-allocated due to libc runtime and the changes of the space due to new, delete the object seen via 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);                                                                                          
 }                             

Upvotes: 2

Views: 582

Answers (1)

Zan Lynx
Zan Lynx

Reputation: 54335

There is no default heap size. The heap is always dynamic and starts at zero. The system calls used are mmap, brk and sbrk.

Most dynamic linked programs use heap in the program loader. They also use it when setting up output buffers for std::cout, FILE *stdout, etc. This is what you see as the "initial heap."

If you built a program without using the C runtime support libraries you would not see any heap usage.

Upvotes: 2

Related Questions