GCC Debug Information Overhead
I was curious how various debug information generation flags affect compile times and disk usage, in particular, how GCC and Binutils respond to different debug level and DWARF compression options.
To test this I decided to build the Linux kernel with all the different flag combinations while measuring the time taken and disk space used by build directory and output vmlinux image.
The GCC source code says the following about debug levels:
-g0
: Write no debugging info-g1
: Write minimal info to support tracebacks only.-g2
: Write info for all declarations (and line table).-g3
: Write normal info plus #define/#undef info.
And compression is controlled with:
-gz=zlib
: (Equivalent to-gz
) Use zlib compression in ELF gABI format-gz=none
: Don’t compress debug sections
Build specifications:
- Toolchain: GCC 10.2.0 and GNU Binutils 2.35.1
- Machine: i7-3770 CPU, 16GB RAM, SSD
- Source code to build: Linux 5.9.11
- Build config: default (x86_64_defconfig)
- Build line:
make --jobs=10 vmlinux O=../linux-build 'KCFLAGS=-g$x -gz=$y' LDFLAGS_vmlinux=--compress-debug-sections=$x
real
is the amount of real time the build tookuser
is the amount of CPU time (in user space) the build tookbuilddir
is the size of../linux-build
vmlinux
is the size of../linux-build/vmlinux
Results:
flags | real | user | builddir | vmlinux |
---|---|---|---|---|
-g0 | 4m46s | 34m52s | 346M | 30M |
-g1 | 4m56s (3% vs g0) | 35m57s ( 3% vs g0) | 444M ( 28% vs g0) | 59M ( 97% vs g0) |
-g2 | 5m52s (23% vs g0) | 40m19s (16% vs g0) | 1422M ( 311% vs g0) | 275M (817% vs g0) |
-g3 | 8m24s (76% vs g0) | 45m54s (32% vs g0) | 4912M (1320% vs g0) | 314M (947% vs g0) |
-g0 -gz | 4m47s | 34m55s | 346M | 30M |
-g1 -gz | 4m58s ( 4% vs g0) ( 1% vs no-gz) | 35m59s ( 3% vs g0) ( 0% vs no-gz) | 423M ( 22% vs g0) ( -5% vs no-gz) | 40M ( 33% vs g0) (-32% vs no-gz) |
-g2 -gz | 6m11s (29% vs g0) ( 5% vs no-gz) | 41m13s (18% vs g0) ( 2% vs no-gz) | 1218M (252% vs g0) (-14% vs no-gz) | 152M (407% vs g0) (-45% vs no-gz) |
-g3 -gz | 8m54s (86% vs g0) ( 6% vs no-gz) | 48m51s (39% vs g0) ( 6% vs no-gz) | 3715M (974% vs g0) (-24% vs no-gz) | 163M (443% vs g0) (-48% vs no-gz) |

So compressing DWARF data doesn't increase build times too much (if at all), but can drastically reduce the amount of disk space used.
The difference between -g2 and -g3 is a lot larger than I expected, both in terms of time and size. This may be because the Linux kernel makes heavy use of macros.
So I think -g2 -gz
will be my goto debug flags from now on :)