Is OS Wasting Memory? Quick Experiment Results

  • Thread starter Thread starter waht
  • Start date Start date
  • Tags Tags
    Memory Os
Click For Summary
SUMMARY

The discussion focuses on memory allocation in C++ programs compiled with the GCC compiler. A simple "Hello World" program resulted in a 9.6 KB executable, despite the source code being only 100 bytes. The analysis revealed a significant portion of the executable, nearly 1 KB, consists of zeros, likely due to default memory allocation settings in the data segment. The user suggests that GCC may have configurable options to reduce memory usage, prompting further investigation into its documentation.

PREREQUISITES
  • Understanding of C++ programming and compilation processes
  • Familiarity with GCC (GNU Compiler Collection) version specifics
  • Knowledge of executable file structures and memory segments
  • Experience with hex editors, such as xxd
NEXT STEPS
  • Research GCC memory allocation flags and options
  • Learn about executable file formats and their memory segments
  • Explore optimization techniques for reducing executable size in C++
  • Investigate the use of hex editors for analyzing binary files
USEFUL FOR

C++ developers, software engineers, and anyone interested in optimizing memory usage in compiled applications.

waht
Messages
1,502
Reaction score
4
I did a quick experiment. First I compiled a simple hello world program in C++ , using a gcc compiler. The source code is 100 bytes, but the executable file it 9.6 kb.

Then I dumbed the executable into hex using xxd, to check up on the 0s and 1s. It turns out there is a huge gap, almost a kilobyte long of zeros.

I'm wondering if this is some sort of delay, or just unused bits?
 
Technology news on Phys.org
I'm guessing that the section with 0s is the data segment of your program, and that unless you specify otherwise, it will allocate a 1K block for it. I don't have any experience with gcc, so don't know if you can configure it to use less memory. Check the docs for gcc.