Fundamentals 7 min read

Why Does Your Executable Size Vary? Understanding BSS, Data, and Text Segments

An overview of the BSS, data, and text segments in program memory—explaining their roles for uninitialized, initialized global variables and executable code, plus heap and stack basics, illustrated with C examples that show how segment placement affects binary size.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Why Does Your Executable Size Vary? Understanding BSS, Data, and Text Segments

BSS Segment

The BSS (Block Started by Symbol) segment stores global variables that are either uninitialized or explicitly initialized to zero. It is part of static memory allocation and does not contain actual data in the executable; the operating system clears it to zero at load time.

Data Segment

The data segment holds global variables that have been initialized to non‑zero values. Like the BSS, it is statically allocated, but its contents are stored in the executable file because the initial values must be preserved.

Text Segment

The text (or code) segment contains the executable instructions of the program. Its size is fixed at link time and is typically read‑only, although some architectures allow writable code sections. Read‑only constants such as string literals may also reside in this segment.

Heap

The heap is the memory area used for dynamic allocation during program execution (e.g., via malloc ). Its size can grow or shrink at runtime.

Stack

The stack stores temporary local variables created inside functions, as well as function parameters, return addresses, and saved registers. It follows a last‑in‑first‑out (LIFO) discipline, making it ideal for call‑site state preservation.

A program is essentially composed of the BSS, data, and text segments. In segment‑based memory management architectures such as Intel x86, the BSS holds uninitialized globals, the data segment holds initialized globals, and the text segment holds the executable code. In such systems, the BSS is not present in the executable file; the loader allocates and zero‑initializes it at runtime, whereas the data and text segments are loaded directly from the executable.

Example

Two small C programs illustrate the effect of segment placement on binary size.

int ar[30000];
void main()
{
    ......
}
int ar[300000] = {1, 2, 3, 4, 5, 6};
void main()
{
    ......
}

Compiling the second program produces a noticeably larger executable. Disassembly with the /FAs option shows the differences:

_BSS SEGMENT
   ?ar@@3PAHA DD 0493e0H DUP (?)  ; ar
_BSS ENDS
_DATA SEGMENT
   ?ar@@3PAHA DD 01H  ; ar
        DD 02H
        DD 03H
        ORG $+1199988
_DATA ENDS

Uninitialized global variables reside in the .bss segment as placeholders.

Initialized global variables reside in the .data segment.

Automatic local variables are allocated on the stack.

The .bss segment does not occupy space in the executable; the OS zero‑initializes it at load time.

The .data segment occupies space because its initial values are stored in the executable.

The .bss segment records only the size needed, not the actual data.

The linker allocates a memory block of the .bss size after the data segment.

.data contains the initialized values, which are present in the object file.

Both .data and .bss together form the program’s data area.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Stackmemory layoutHeaptext segmentbss segmentData Segment
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.