Your Search Bar For Shrewd Tips

How Do I Clear Zram?


Zram is a Linux feature that creates a compressed block device in RAM. Many systems use it as compressed swap, which can help when memory is tight. If you want to clear zram, the safe method is usually to deactivate it as swap first, then reset the device. This lets Linux move any pages stored there back into regular memory before the compressed data is discarded.

The exact steps depend on how your distribution configured zram, and clearing it can fail—or cause memory pressure—if the device is busy or your system does not have enough available RAM. This guide explains how to identify your zram device, clear it safely, check that it worked, and troubleshoot common issues.

What Clearing Zram Does

When zram is used as swap, Linux compresses pages and stores them in a block device backed by RAM. Clearing zram removes the data stored on that device and resets its state. It does not erase files on your disk, and it does not normally require a reboot.

Before resetting a zram swap device, turn off that swap device with swapoff. The kernel can then move its pages back into regular memory. Resetting an active device can fail because it is in use, and trying to deactivate it can fail if there is not enough memory to hold the pages. If the zram device is being used for something other than swap, such as a filesystem, you must stop using that workload and unmount it before resetting the device.

1. Find the Zram Device

Start by checking which swap devices are active. You do not need to guess whether the device is called /dev/zram0; inspect your system first.

swapon --show
zramctl

The swapon --show command lists active swap areas. A zram device commonly appears as /dev/zram0, though a system can have more than one, such as /dev/zram1. The zramctl command shows zram devices and their status. On some systems, you may need administrator privileges to see all details:

sudo zramctl

If you want to inspect device names and swap use together, try:

cat /proc/swaps
ls /dev/zram*

If zramctl reports no device or /dev/zram* does not exist, zram may not be enabled or available on your system. Do not run reset commands against a device name you have not confirmed.

2. Check Available Memory

Turning off zram swap can require Linux to bring its stored pages back into RAM. Check memory before proceeding, especially if the system is already under pressure or running memory-intensive applications.

free -h

Look at the available-memory figure, not just the amount shown as free. Linux uses otherwise idle memory for caches, and some of that can be reclaimed. Still, if available memory is low, swapoff may fail or the system may become unresponsive. Close demanding applications or wait until memory use drops. If the machine is critically low on memory, avoid forcing the operation.

3. Turn Off the Specific Zram Swap Device

Use swapoff on the zram device shown in your output. For example, if the active device is /dev/zram0, run:

sudo swapoff /dev/zram0

Replace /dev/zram0 with the actual device name on your system. This command deactivates that swap area, allowing the kernel to move its pages elsewhere. It may take a little time, depending on how much swap is in use and the available memory.

Check that it is no longer active:

swapon --show

If /dev/zram0 is absent from the output, it is no longer active as swap. Avoid using sudo swapoff -a just to clear one zram device: that command turns off every active swap area, including disk-backed swap, and can create unnecessary memory pressure.

4. Reset the Zram Device

Once the zram device is no longer active as swap, reset it with zramctl:

sudo zramctl --reset /dev/zram0

Again, replace the example device with the one you identified. Resetting discards the contents and resets the zram device’s state. On systems with an older version of zramctl that does not support the --reset option, consult that version’s manual or your distribution’s documentation. Do not try random commands against the zram sysfs controls; use a method supported by your system.

Check the result:

sudo zramctl
swapon --show

The device may still appear in zramctl after a reset. That does not necessarily mean it still contains old swap data: zram can remain configured and ready for use while its previous contents have been cleared. The important checks are that the reset completed without an error and that the device is not unexpectedly active in swapon --show.

5. Turn Zram Swap Back On, If Needed

Resetting a device does not always reactivate it as swap. If you want to keep using zram, turn the same device back on with swapon:

sudo swapon /dev/zram0

Then verify its status:

swapon --show
zramctl

If your distribution manages zram automatically, it may configure or activate the device through a service or a boot-time generator. In that case, the system’s normal management process may be a better way to restore its intended setup than manually activating it. Check your distribution’s documentation if the device does not return to its expected state.

Clearing Zram Without Disabling It Permanently

For a one-time cleanup, deactivating the device, resetting it, and reactivating it is generally different from permanently disabling zram. The sequence clears the current contents; it does not necessarily change what happens at the next boot.

If you want zram to stay disabled, find out what configures it before changing system settings. Depending on the distribution, zram may be managed by a zram-generator configuration, a systemd service, a startup script, or another package. Changing or removing the wrong configuration can affect other swap areas or prevent the system from recreating zram correctly.

Use these commands to look for relevant services and configuration files:

systemctl list-units --type=service | grep -i zram
systemctl list-unit-files | grep -i zram
ls /etc/systemd/zram-generator.conf /etc/systemd/zram-generator.conf.d 2>/dev/null

These checks can help you identify how zram is managed, but the names and paths vary. Do not disable a service while its device is still active as swap, and do not remove configuration unless you intend to change zram’s behavior on future boots. Follow your distribution’s instructions for making a permanent change.

Common Problems and Safe Fixes

  • swapoff reports that there is not enough memory: Linux cannot move the pages out of zram with the memory currently available. Close applications, reduce memory use, or wait and try again. Do not repeatedly force the operation on a system that is becoming unstable.
  • zramctl --reset says the device is busy: Confirm that the device is no longer listed in swapon --show. If it is used for a filesystem or another purpose, stop that use and unmount it before resetting.
  • The device reappears in swap output: A system service or configuration may have activated it again. Check how your distribution manages zram and whether an automated process is running.
  • The device is missing: Zram might not be configured, or the device might have a different name. Check zramctl, /proc/swaps, and your distribution’s setup instructions.
  • The command is not available: The zramctl utility is commonly provided by the util-linux package. Package names and installation commands differ between distributions; use your system’s package manager documentation.
  • The machine becomes slow during the process: Moving pages back into RAM can increase memory pressure and disk activity if other swap is used. Give the operation time, and avoid running additional memory-heavy tasks.

Frequently Asked Questions

Will clearing zram delete my files? No. Resetting a zram swap device clears temporary memory contents, not files stored on a disk. However, unsaved work can still be lost if the system crashes during a memory emergency, so save your work before starting.

Do I need to reboot to clear zram? Usually not. If the device is active as swap, use swapoff, reset it, and reactivate it if you want it to remain in use. A reboot is not a substitute for checking which device is active or how your system manages it.

Can I reset zram while it is active? Do not try to reset an active swap device. Deactivate it first so the kernel can move its pages elsewhere. If the device is being used for a different purpose, stop that use before resetting it.

Does clearing zram improve performance? Not usually as a routine maintenance step. Zram is designed to store compressed data in RAM and can help a system use memory more effectively. Reset it when you have a specific reason, such as troubleshooting or deliberately clearing its current contents.

Conclusion

To clear zram safely, identify the correct device, check that the system has enough available memory, turn off that specific device with swapoff, and reset it with zramctl --reset. Verify the result, then reactivate the device if you still want zram swap. If a command fails, check for low memory or another active use of the device rather than forcing a reset. This approach clears zram’s current contents while helping protect your running system and its other swap areas.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →