---
title: "配置zram"
id: "3337"
type: "post"
slug: "%e9%85%8d%e7%bd%aezram"
published_at: "2023-03-08T06:50:06+00:00"
modified_at: "2023-03-08T07:04:39+00:00"
url: "https://seq.ink/all/3337.html"
markdown_url: "https://seq.ink/all/3337.html.md"
excerpt: "Linux启用zram"
taxonomy_category:
  - "ALL"
  - "Linux"
---

> 参考原文：[https://segmentfault.com/a/1190000041578292](https://segmentfault.com/a/1190000041578292)

### 配置开机加载zram模块

```
echo 'zram' | sudo tee /etc/modules-load.d/zram.conf
```

### 配置udev自动创建4G大小的zram设备

PS：可以自己改成其它比如256K 512M 1G

```
echo 'KERNEL=="zram0",ATTR{disksize}="4G",TAG+="systemd"' | sudo tee /etc/udev/rules.d/99-zram.rules
```

### 配置自动设置为SWAP交换分区配置自动设置为SWAP交换分区

```
cat << EOF | sudo tee /etc/systemd/system/zram.service
[Unit]
Description=ZRAM
BindsTo=dev-zram0.device
After=dev-zram0.device

[Service]
Type=oneshot
RemainAfterExit=true
ExecStartPre=/sbin/mkswap /dev/zram0
ExecStart=/sbin/swapon -p 2 /dev/zram0
ExecStop=/sbin/swapoff /dev/zram0

[Install]
WantedBy=multi-user.target
EOF
```

### 设置为开机自启

```
sudo systemctl enable zram.service
```
