size Command in Linux



The size command in Linux is a powerful utility that provides information about the memory consumption of object files or executables. It displays the sizes of different sections in a file, such as text (code), data (initialized variables), and bss (uninitialized variables) segments, along with the total size. This command is particularly useful for developers and system administrators who want to analyze and optimize the memory usage of programs.

Table of Contents

Here is a comprehensive guide to the options available with the size command −

Syntax of size Command

The basic syntax for the size command is −

size [options] [file...]
  • file − Specifies the object file(s) or executable(s) to analyze.
  • options − Modifies the output or behavior of the command.

Sections Displayed by size Command

When you run the size command, it outputs the sizes of the following sections −

  • Text − Contains the program's executable code.
  • Data − Includes initialized global and static variables.
  • BSS − Represents uninitialized global and static variables, which don’t take up space in the executable but are allocated during runtime.
  • Total − Summation of all sections to give the total memory size.

size Command Options

Here are several handy options you can try with the size command on Linux –

Option Description
-A or --format=sysv Displays the output in a format resembling System V style, which includes a detailed breakdown of sections and their sizes.
-B or --format=berkeley Produces output in Berkeley style (default), showing text, data, bss, dec, and hex columns in a single line.
-G or --format=gnu Outputs information in GNU format, which is similar to Berkeley but with text, data, and bss sizes summed up into a single total column.
-d or --radix=10 Displays sizes in decimal format.
-o or --radix=8 Shows sizes in octal format.
-x or --radix=16 Outputs section sizes in hexadecimal format.
--help, -h, -H, or -? Displays a help message summarizing all available options.
-t or --totals Displays totals for all objects listed, only in Berkeley or GNU formats.
--common Includes the total size of common symbols in the bss size when using Berkeley or GNU format.
--version, -v, or -V Displays the version of the size command you are using.
@file Reads additional options from the specified file.

Examples of size Command in Linux

Here are several hands-on use cases of the Linux size command for better understanding −

  • Analyzing a Single Executable
  • Comparing the Size of Multiple Object Files
  • Displaying Sizes in System V Format
  • Adjusting Output Radix to Hexadecimal
  • Checking Sizes of a Debug Build

Analyzing a Single Executable

Let’s say you have a compiled executable named myprogram, and you want to check how much memory each section is using. You can run −

size myprogram

This command outputs the sizes of the text, data, and bss sections, along with the total memory usage in both decimal and hexadecimal formats.

size Command in Linux1

Comparing the Size of Multiple Object Files

Suppose you want to compare the memory consumption of two object files, file1.o and file2.o, to see which file uses more resources. Run this command −

size file1.o file2.o

The output will provide a breakdown of the memory usage for both files, making it easy to compare their text, data, and bss sizes, as well as the total usage.

size Command in Linux2

Displaying Sizes in System V Format

Let’s say you need detailed information about the memory sections in an object file, displayed in a System V style. Use this command −

size -A file1.o

This option (-A) presents a detailed breakdown of each section and its memory address.

size Command in Linux3

Adjusting Output Radix to Hexadecimal

Suppose you want to see the memory sizes in hexadecimal format, which is useful for low-level debugging. Run the following −

size -x myprogram

The output will show the sizes of all sections in hexadecimal.

size Command in Linux4

Checking Sizes of a Debug Build

Suppose you compiled a debug version of your program named debug_program, and you want to verify how much memory it occupies. Run −

size debug_program

This command shows the breakdown of text, data, and bss sections for the debug build.

size Command in Linux5

Conclusion

The size command in Linux is an essential tool for developers and system administrators to analyze and optimize memory usage in programs. By providing detailed insights into the text, data, bss, and total memory sections of object files and executables, it empowers users to make informed decisions about memory efficiency.

Whether you're debugging, comparing builds, or analyzing system performance, mastering the size command ensures better control over resource allocation and program optimization.

Advertisements