Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prolong lifetime of MacBook's SSD by disabling swap and other minor tweaks - From Y to X --- 通过禁用交换和其他小调整来延长 MacBook SSD 的使用寿命 - 从 Y 到 X #84

Open
eightHundreds opened this issue Apr 6, 2023 · 0 comments

Comments

@eightHundreds
Copy link
Owner

0. Prologue


After replacing 128GB SSD with a 500GB one in my 7 year-old MacBook Air, I’ve noticed a serious issue… Since 4GB RAM is very limited (and not designed) for the contemporary macOS, it runs out of memory very often especially when I opens multiple tabs in browser and edits photo in Lightroom simultaneously. It isn’t a big deal because there is swap mechanism, which stores data to disk when the (physical) memory is not enough. However, since the SSDs have finite numbers of P/E (program / erase) cycles, the more intensive the swap uses, the sooner the SSD would be worn out. The lifetime of SSD basically depends on its type of NAND flash. For the SSD I installed, WD SN750 500GB, it uses TLC NAND and declares it has 300TB-write endurance.
在我用了 7 年的 MacBook Air 上用 500GB SSD 替换了 128GB SSD 之后,我注意到了一个严重的问题……由于 4GB RAM 对于现代 macOS 来说非常有限(而且不是设计的),它经常耗尽内存,尤其是当我在浏览器中打开多个选项卡并同时在 Lightroom 中编辑照片。这没什么大不了的,因为有交换机制,当(物理)内存不足时,它会将数据存储到磁盘。但是,由于 SSD 的 P/E(编程/擦除)循环次数有限,因此交换使用的次数越多,SSD 磨损得越快。 SSD 的寿命基本上取决于其 NAND 闪存的类型。对于我安装的SSD,WD SN750 500GB,它使用TLC NAND并声称它具有300TB的写入耐力。


SMART information checked with smartmontools
使用 smartmontools 检查 SMART 信息

Unfortunately, if the SSD is used as system disk with limited RAM, it will be worn out rapidly by the swap mechanism. For example, my MacBook with new SSD has powered on for 161 hours, and it has been written for ~663GB data(!). To tackle this issue, I should either upgrade my MacBook ($) or try to limit the usage of memory by disabling swap and doing minor tweaks.
不幸的是,如果SSD用作RAM有限的系统盘,它会被交换机制迅速磨损。例如,我的带有新 SSD 的 MacBook 已开机 161 小时,并且已写入约 663GB 数据(!)。要解决此问题,我应该升级我的 MacBook ($) 或尝试通过禁用交换和进行小的调整来限制内存的使用。

1. Disable swap1.禁用交换

Be careful! Disable swap would cause kernel panic if there is not enough RAM for processes. Take it at your own risk.
当心!如果没有足够的 RAM 供进程使用,禁用交换将导致内核崩溃。自行承担风险。

1.0. Disable SIP1.0。禁用 SIP

From macOS 10.11 to present, there is a built-in system integrity protection (SIP) for preventing malicious modification. To disable swap, first we have to turn SIP off.
从 macOS 10.11 到现在,有一个内置的系统完整性保护(SIP)来防止恶意修改。要禁用交换,首先我们必须关闭 SIP。

  1. Power-off MacBook first, hold Command (⌘) and R, and then start-up the MacBook. For M1 chip based MacBook, hold the power button until seeing “Loading startup options”.
    首先关闭 MacBook,按住 Command (⌘) 和 R,然后启动 MacBook。对于基于 M1 芯片的 MacBook,按住电源按钮直到看到“加载启动选项”。
  2. You should see a macOS utilities window.
    您应该会看到一个 macOS 实用程序窗口。
  3. From the utilities menu, select “Terminal”
    从实用程序菜单中,选择“终端”
  4. Type **_csrutil disable_** and press return, the screen should display a message that SIP is already disabled.
    键入 **_csrutil disable_** 并按回车键,屏幕应显示 SIP 已被禁用的消息。
  5. From the  menu, press restart to restart MacBook.
    在  菜单中,按下重新启动以重新启动 MacBook。


Select “Terminal” from the menu bar
从菜单栏中选择“终端”


Disable SIP via Terminal通过终端禁用 SIP

1.1. Disable swap1.1.禁用交换

To check the current mode that system manages memory, type sysctl -a vm.compressor_mode in terminal. The default mode is 4, which represents compressing memory and using swap. To disable swap, we should change the mode from 4 to 2, which means compressing memory only.
要检查系统管理内存的当前模式,请在终端中键入 sysctl -a vm.compressor_mode 。默认模式是 4 ,代表压缩内存,使用swap。要禁用交换,我们应该将模式从 4 更改为 2 ,这意味着仅压缩内存。

To modify vm.compressor_mode, type sudo nvram boot-args="vm_compressor=2". The mode won’t be changed until reboot. Again, anyone running computer without swap file should monitor memory usage carefully to avoid kernel panic!
要修改 vm.compressor_mode ,请键入 sudo nvram boot-args="vm_compressor=2" 。在重新启动之前,模式不会更改。再次重申,任何运行没有交换文件的计算机都应该仔细监控内存使用情况以避免内核崩溃!


Type csrutil status first to ensure that SIP is already disabled
首先键入 csrutil status 以确保 SIP 已被禁用

1.2. Enable SIP (Recommended, for macOS 10.x only)

1.2.启用 SIP(​​推荐,仅适用于 macOS 10.x)

Note that this only is for macOS 10! If you are using Apple Silicon MacBook (M1) or macOS 11+, enabling SIP will also change back vm.compressor_mode to default.
请注意,这仅适用于 macOS 10!如果您使用的是 Apple Silicon MacBook (M1) 或 macOS 11+,启用 SIP 也会将 vm.compressor_mode 改回默认值。

After disabling swap, we should enable system integrity protection (SIP) again for security reason. The steps are similar to disabling swap, and the only difference is the command. Type csrutil enable instead to enable SIP. If you want to disable spindump furtherer (listed below), you should enable SIP later.
禁用交换后,出于安全原因,我们应该再次启用系统完整性保护(SIP)。步骤与禁用交换类似,唯一不同的是命令。改为键入 csrutil enable 以启用 SIP。如果你想进一步禁用 spindump (如下所列),你应该稍后启用 SIP。

2. Disable unused processes

2.0. Disable spindump and tailspind

Here we use launchctl to unload two services, and rename the files.

  1. sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.spindump.plist

  2. sudo mv /System/Library/LaunchDaemons/com.apple.spindump.plist /System/Library/LaunchDaemons/com.apple.spindump.plist.bak

  3. sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.tailspind.plist

  4. sudo mv /System/Library/LaunchDaemons/com.apple.tailspind.plist /System/Library/LaunchDaemons/com.apple.tailspind.plist.bak

2.1. Disable mds_stores

mds stands for “metadata server”, which is a part of Spotlight in macOS. It indexes for all devices to provide effective search functionality. If you never use Spotlight, you could disable it for saving CPU and memory usage.

To prevent it from indexing all devices, type sudo mdutil -a -i off in terminal.

If you want to enable it again, just replace the command with sudo mdutil -a -i on.

2.2. Disable Adobe CC services

Although Adobe provide amazing products like Lightroom and Photoshop, its background services consume memory and CPU even when the Adobe-related program is not running.

The service corresponding to Adobe CC services is located at /Library/LaunchAgents/com.adobe.AdobeCreativeCloud.plist, to prevent it from auto-launching from log-in, we will use launchctl again: type launchctl unload -w /Library/LaunchAgents/com.adobe.AdobeCreativeCloud.plist in terminal.

To enable it, just replace unload with load in the command.

3. Tab suspender


If you open a lot of tabs in the browser every time like me, you should consider suspending inactive tabs while you are not browsing the website or AFK, to limit the memory usage of browser. The following method is recommended. It is risky to use third-party extensions such as The Great Suspender since it might contain malware.

Microsoft’s Edge has announced built-in “sleeping tab” feature recently (2020/09). To enable it, simply go to Settings → System → Save resources.


Furthermore, you can modify timeout to suspend the inactive and set the allow-list in the configuration page.


The disk health information can be inspected with smartmontools. To use it on macOS, either install precompiled package here (download smartmontools-x.x-x.dmg one) or install via brew:

brew install smartmontools

After installing, you can run the command in Terminal.app:

smartctl -a disk0

where disk0 is the device name referring to the inspected disk. Device name can be found via Disk Utility.app:


Disk Utility.app


Device name shown in bottom right corner.

Device information such as serial number, temperature, bytes written and read will be shown in the terminal. Note that some NVMe admin commands are not supported currently via smartctl, therefore some detailed NVMe information will not be shown correctly.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant