How to Use the stringdups Command in macOS Terminal to Identify Duplicate Strings and Optimise Process Memory

When profiling native Swift, Objective-C, and C++ applications on macOS, developers and reverse engineers often focus heavily on heap leak detection and zombie retain cycles. However, one of the most insidious contributors to excessive process resident memory (RSS) is string duplication. In large enterprise applications, localized UI strings, configuration keys, JSON dictionaries, and logging identifiers are frequently allocated repeatedly as distinct heap instances rather than sharing immutable memory buffers.

macOS includes a specialized command-line diagnostic tool engineered specifically to detect and quantify redundant strings: stringdups. Located in /usr/bin/stringdups, this tool inspects a running process or memory dump, extracts all internal string objects (including NSString, CFString, and raw C-strings), and generates a ranked report of duplicate allocations and wasted byte counts.

Understanding Memory String Deduplication

In the Apple Darwin Mach-O runtime, immutable strings can ideally be uniqued via string interning (or mapped memory sections). When applications dynamically construct strings at runtime, each allocation consumes an object header, pointer metadata, and character payload. If an application loads 50,000 identical localized strings from a remote API response, thousands of kilobytes of RAM are wasted needlessly.

Running stringdups in Terminal

To run stringdups against a target application, identify its Process ID (PID) via pgrep or Activity Monitor:

# Identify PID
pgrep -x Safari

# Run stringdups against PID
stringdups <PID>

Filtering and Ordering Duplicate Reports

1. Sorting by Total Wasted Bytes

To immediately highlight strings causing the largest memory overhead, sort the analysis by total wasted space:

stringdups -minimumCount 5 -order wasted <PID>

The -minimumCount 5 argument filters out one-off strings and reports only instances duplicated five or more times.

2. Inspecting Memory Allocation Call Stacks

If you have enabled Malloc Stack Logging for the target process in Terminal (export MallocStackLogging=1), stringdups can print the exact function call stack responsible for generating each duplicated string:

stringdups -callTree <PID>

Interpreting stringdups Output

The standard output displays several diagnostic columns:

Column Header Definition Diagnostic Significance
Count Number of separate instances Shows how widely the string is proliferated across heap nodes
Bytes Total size consumed across instances Quantifies cumulative memory consumption
Wasted Bytes Bytes wasted beyond a single instance Shows direct RAM reclaimable through string interning
String Value Literal UTF-8 / ASCII string Identifies the exact configuration key or UI label

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.