Minimum memory usage 504 KB

While doing some experiments while studying C, I came across something strange. This is my program:

int main(void) {sleep(5);} 

When it is compiled, the file size of the executable file is 8496 bytes (compared to the 26-byte source!) This is understandable when you call sleep and call instructions that are written in the executable file. Another point is that without sleep, the executable becomes 4312 bytes.

 int main(void) {} 

My main question is what happens when the first program starts. I use clang to compile and Mac OS X to run it. The result (according to Activity Monitor) is that the program uses 504 KB of "real memory". Why is it so large when the program is only 4 KB? I assume the executable is loaded into memory, but I have done nothing but call sleep. Why does my program need 500 KB for sleep in five seconds?

By the way, the reason I use sleep is to catch the amount of memory used with Activity Monitor.

I ask simply out of curiosity, cheers!

+8
c memory
source share
2 answers

When you compile a C program, it is associated with an executable file. Although your program is very small, it will reference the C runtime, which will contain some additional code. Some error handling may occur, and this error handling may be written to the console, and this code may include sprintf , which adds some trace to your application. You can request the linker to create a code map in your executable file to see what is actually included.

In addition, the executable file contains more machine code. There will be various tables for data and dynamic linking that increase the size of the executable, and there may also be some wasted space because the various parts are stored in blocks.

The C runtime will be initialized before the main call, and this will lead to loading of some loadable code (for example, by dynamically linking to various functions of the operating system), as well as memory allocated for the heap, stack for each thread, and possibly also some static data. Not all of this data can be displayed as β€œreal memory” - the default stack size for OS X is 8 MB, and your application still uses much less than that.

+7
source share

In this case, I assume that the size difference that you observed is significantly caused by dynamic linking.

Typically, linkers do not put common code in executable binaries; instead, they back up information and the code is loaded when the binary is loaded. Here, this shared code is stored in files named shared object(SO) or dynamically linked library(DLL) .

 [pengyu@GLaDOS temp]$ cat test.c int main(void) {sleep(5);} [pengyu@GLaDOS temp]$ gcc test.c [pengyu@GLaDOS temp]$ du -h --apparent-size a.out 6.6K a.out [pengyu@GLaDOS temp]$ gcc test.c -static [pengyu@GLaDOS temp]$ du -h --apparent-size a.out 807K a.out 

In addition, here I list what is in the process memory:

  • You need dynamic libraries to load:

    Here ldd gives the result of loading dynamic libraries when calling a binary file. These libraries are located in the part where it was received, calling the mmap system call.

     [pengyu@GLaDOS temp]$ cat test.c int main(void) {sleep(5);} [pengyu@GLaDOS temp]$ gcc test.c [pengyu@GLaDOS temp]$ ldd ./a.out linux-vdso.so.1 (0x00007fff576df000) libc.so.6 => /usr/lib/libc.so.6 (0x00007f547a212000) /lib64/ld-linux-x86-64.so.2 (0x00007f547a5bd000) 
  • Partitions of the type .data , .code are allocated for data from your binary file.

    This part exists in the binary executable, so the size should not be lager than the file itself. The content is copied at the download stage of the executable binary.

  • There are sections of type .bss , as well as a stack area that will be allocated for dynamic use during program execution.

    This part does not exist in the binary executable, so the size can be quite large without affecting the size of the file itself.

+2
source share

All Articles