Does optimizing for size with gcc -Os only reduce the binary size of a program, or does it reduce its runtime memory usage as well? I know what exactly the results are depend on the specific code, but in general is the result a lower memory usage?


Obviously, since the program needs to be loaded into memory, -Os will result in lower memory usage. But that is the only effect on memory usage it will have.

| improve this answer | |
  • If it's smaller wouldn't it mean the internal wiring is also different and potentially faster, potentially using less memory in other ways? – Caleb May 26 '11 at 12:38
  • I don't think so. The size of the code is reduced, not the size of the data structures. – Kim May 26 '11 at 13:02
  • 1
    Its possible to reduce the size of the data structures by packing things closer together. This may make the code slower, since it has to unpack at runtime. I'm not sure if -Os actually does this, but it is possible. – KeithB May 26 '11 at 13:31
  • 1
    @KeithB: The man page of gcc clearly says -Os optimizes for code size. Packing and unpacking data structures would increase code size. – Kim May 26 '11 at 14:27
  • 1
    While possible in theory, changing the layout would break compatibility with binaries compiled without -Os. There is also the question of what the C standard requires. – Kim May 26 '11 at 15:11

One aspect is that -Os will try to give you a smaller application, and since it is smaller sometimes it will execute faster since there is less code to execute.

| improve this answer | |
  • Can you expand this answer and defend it against the conflicting information in Kim's answer? – Caleb May 26 '11 at 12:38
  • It has a lot to do on what platform, and what kind of code is compiled. Sometimes it is faster to use a bigger binary with a lot of in-lined code, and sometimes that in-line cost time to execute. It depends amongst other things on things like the size of the pipeline in the cpu and the cost of cache misses etc etc. Look at the extremes and compare a Desktop PC with a AVR mcu, the -Os will have quite different behaviour on the system. It is not black and white... – Johan May 26 '11 at 12:48
  • Here's one reference: x264dev.multimedia.cx/archives/category/gcc. But on the contrary: transient-inode.blogspot.com/2009/03/… – mattdm Aug 8 '11 at 14:43

Actually, according to some sources like gentoo-wiki, the -Os flag can have more benefits than just reduced binary footprint. But extensive usage of it can cause problems. The actual benefits can be seen for large programs, but as the flag can trigger some bugs, one must be careful not to use it with unstable code. So, unfortunately, this means that really large pieces of software are more likely to expose bugs (by pure probability: more code => higher risk of bugs exposure). Many large applications filter this flag out at build-time to prevent such bugs from being exposed.

Also keep in mind that -Os only can make an application faster - this is not a rule. What is certain about it is that the compiler will try to make the program smaller.

| improve this answer | |

As Kim says, the resulting binary is smaller, so the stack memory usage will be smaller too.

It might also be faster than some other optimisation flags, because more of the program will fit into the CPU caches. That's one reason it has become more popular of late.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.