Table of contents
# How to enable Zram in Linux
TL;DR / [Geek Summary]:
- Memory Alchemy: Enable Zram to create compressed RAM devices, boosting multitasking limits without physical upgrades.
- Performance Trade-off: Unlike disk-based Swap, Zram trades minor CPU cycles for lightning-fast I/O, perfect for low-end VPS or IoT.
- One-Click Deployment: Use community scripts to deploy a Zram+Swap combo, providing elastic hedging against memory pressure.
Zram is usually used in embedded devices such as Android phones. For example, in a 2.0GB running memory, there are 1.8 real running memory and 0.2 zram memory to make up the number. Another advantage of zram is that it does not occupy hard disk space or wear hard disk
# The difference between zRAM and Swap
zRAM and traditional swap space (Swap) are technologies used by Linux systems to increase available memory, but their working methods and usage scenarios are different.
Traditional Swap Space (Swap):
Storage Medium: Swap usually uses a hard disk drive (HDD) or solid-state drive (SSD) as a swap area.
Performance: Because hard disks are much slower than RAM, using swap may significantly slow down the system, especially when memory is exhausted.
Reliability: Swap is on a different physical medium, so it does not affect the life of RAM.
Purpose: It can be used as an overflow area when physical memory is exhausted, but due to its slow speed, it is usually only suitable for very low memory pressure.
zRAM:
Storage Medium: zRAM is a compressed RAM block device that uses the memory itself to store compressed data.
Performance: zRAM reads and writes much faster than traditional swap because it operates directly on RAM. Compressing and decompressing data requires some CPU resources, but usually this overhead is less than the time to read and write data from the hard disk.
Reliability: Because zRAM uses RAM, frequent reading and writing may affect the life of RAM, although the actual impact is very small.
Purpose: zram is very suitable for devices with limited RAM or that need a fast swap area, such as embedded devices or old computers, and is also commonly used in Android devices.
# Summary:
zRAM provides a way to increase the effective memory capacity without increasing physical memory, by creating a compressed block device in RAM.
Traditional swap space (Swap) is a part of the hard disk that is used as virtual memory when the system’s physical memory is insufficient.
zram is particularly suitable for systems with small memory because it can add some “virtual” memory with little performance overhead, although this will increase the burden on the CPU.
Traditional swap is suitable for situations where there is enough hard disk space and the memory pressure is not very large, especially when the system load is not high, but occasionally more memory is needed.
The best practice may be to use zRAM and traditional swap together for more flexible and optimized performance. When memory is tight, zRAM can provide faster data exchange, and when zRAM is also exhausted, traditional swap can be used as a last backup.
# One-click script setting
https://github.com/spiritLHLS/addzram
https://github.com/spiritLHLS/addswap
# Swap memory (occupies hard disk space)
Update time: 2023.08.27
Add swap partition (virtual memory) for openvz and kvm virtualized linux servers
| |
Added automatic addition of swap when restarting the openvz architecture
Openvz’s addition = stealing the bell from the ears, which is actually due to virtualization restrictions and should not be added. It can only be controlled by the virtualized host. Similarly, LXC virtualization can only be controlled by the host, and virtual memory SWAP cannot be added autonomously
Unit conversion: input 1024 to generate 1G SWAP memory
# Zram memory (CPU usage)
Updated: 2023.08.30
Enable zram device (compressed memory) for linux server
| |
It’s just that zram is compressed memory usage, and swap is additional virtual memory to increase memory space. Both will occupy CPU resources. zram is better used on machines with redundant CPU performance
(In theory, zram will have lower performance usage than swap, but it has not been actually tested)
Unit conversion: Enter 1024 to generate 1G of zram device compressed memory. The size of the zram device should not be larger than the actual memory size.
