Configure 7-Zip Multi-Threading to Avoid Out of Memory
When compressing large files with 7-Zip, high thread counts paired with large dictionary sizes can quickly consume all available system RAM, resulting in "Out of memory" errors or system instability. This article explains how memory allocation works in 7-Zip's LZMA and LZMA2 algorithms and provides actionable steps to configure thread and dictionary settings in both the Graphical User Interface (GUI) and Command-Line Interface (CLI) to keep memory usage within safe limits.
Understanding 7-Zip Memory Consumption
7-Zip's default LZMA2 compression algorithm splits files into chunks and processes them simultaneously across multiple CPU threads. Memory consumption is dictated primarily by two parameters:
- Dictionary Size: The amount of data stored in memory to identify duplicate patterns.
- Thread Count: The number of parallel processing threads used simultaneously.
In LZMA2, the compression memory requirement roughly scales as:
Memory Usage ≈ Dictionary Size × Number of Threads × 2
(plus additional overhead).
For example, using a 64 MB dictionary across 16 threads can require over 2 GB to 3 GB of RAM for the compression phase alone, while a 256 MB dictionary across 16 threads can exceed 8 GB of RAM.
Method 1: Adjust Parameters in the 7-Zip GUI
To manage resource usage when using the desktop application:
- Right-click the files or folders you want to compress and select 7-Zip > Add to archive....
- Locate the Compression method dropdown and ensure it is set to LZMA2 (which handles multithreading far more efficiently than the older LZMA).
- Look at the dynamic Memory usage for Compressing field in the lower-right section of the window.
- Reduce the Number of CPU threads:
- Instead of using your maximum logical core count, set the value to half your available cores (e.g., 2, 4, or 6 threads).
- Reduce the Dictionary size:
- Lower the dictionary size (e.g., from 64 MB down to 16 MB or 32 MB). Watch how the estimated memory usage indicator decreases immediately.
- Ensure that the estimated memory usage does not exceed 70% of your total free physical RAM to leave room for the operating system and background processes.
- Click OK to start the compression.
Method 2: Configure Multi-Threading in the Command Line (CLI)
When using 7z or 7za from the terminal or
in automated scripts, use dedicated switches to prevent memory
exhaustion:
1. Limit Thread Count
(-mmt)
The -mmt switch defines the number of CPU threads
allocated to the operation.
- Limit to a specific number of threads (e.g., 2 threads):
7z a -t7z archive.7z /path/to/files -mmt=2 - Disable multi-threading entirely (single-thread mode):
7z a -t7z archive.7z /path/to/files -mmt=off
2. Restrict Dictionary Size
(-md)
The -md switch controls the dictionary size, drastically
decreasing the memory multiplier per thread.
- Set a 16 MB or 32 MB dictionary:
7z a -t7z archive.7z /path/to/files -md=32m
3. Combined Safe Compression Command
Combine -mmt and -md with the LZMA2 method
(-m0=lzma2) for stable execution in memory-constrained
environments:
7z a -t7z archive.7z /path/to/files -m0=lzma2 -mmt=4 -md=32mRecommended Configurations by System RAM
- 4 GB to 8 GB RAM Systems: Set
-mmt=2with a dictionary size of16mor24m. - 16 GB RAM Systems: Set
-mmt=4or-mmt=6with a dictionary size of32mor64m. - Batch Scripts and Servers: Avoid omitting the
-mmtflag; auto-detection selects all threads, which risks crashing background environments under load.