MacOS 控制固态磁盘写入量,设置定时任务监控

M1 芯片的内存交换策略非常激进,导致 内存较小的机型 固态硬盘写入量十分恐怖,网上很多人都有类似的遭遇。

如何看待 8G 256G M1 MacBook Air 使用一个月硬盘写入 22TB+?

而固态硬盘是有擦除、写入寿命的,一般就按100次算,256G 大概就是 250TB。当然,并不是说超过这个数,硬盘就坏了,只是一般超过这个数,再坏,厂商就不包了。具体不同型号、不同厂商也会给不同的 TBW (Total Bytes Written)

查询磁盘写入量

  1. 安装磁盘工具smartmontools:
bash 复制代码
brew install smartmontools
bash 复制代码
smartctl -a disk0

Data Units Written 就是磁盘写入量,10个月左右的写入量 39 T

plaintext 复制代码
=== START OF SMART DATA SECTION ===
SMART overall-health self-assessment test result: PASSED

SMART/Health Information (NVMe Log 0x02)
Critical Warning:                   0x00
Temperature:                        34 Celsius
Available Spare:                    100%
Available Spare Threshold:          99%
Percentage Used:                    2%
Data Units Read:                    127,486,481 [65.2 TB]
Data Units Written:                 76,316,904 [39.0 TB]
Host Read Commands:                 1,790,447,762
Host Write Commands:                694,652,609
Controller Busy Time:               0
Power Cycles:                       197
Power On Hours:                     934
Unsafe Shutdowns:                   8
Media and Data Integrity Errors:    0
Error Information Log Entries:      0

设置定时任务监控磁盘写入量

写一个 python 脚本 smart-daily.py,放在合适的位置,log 的目录,自己看着改。

chmod+x smart-daily.py 添加执行权限。第一行不能删,否则不能直接 smart-daily.py 执行。

python 复制代码
#!/usr/bin/env python
import os
import time
import re

command_result = os.popen("smartctl -a disk0").read()
lines = command_result.splitlines()

for line in lines:
    if line.find("Data Units Written") != -1:
        # print(line)
        start_index = re.search(r"\d", line).start()
        line = line[start_index:]
        with open('DataWritten.log', mode='a', encoding='utf-8') as f:
            f.write(time.strftime("%Y-%m-%d %H:%M:%S: ",
                    time.localtime()) + line + "\n")

定时任务的设置参考:mac 定时执行python脚本

如何减少磁盘写入量?

根本的办法是加钱买更大内存,此外,以下几种方法

注意保护

实测一段时间,上面的方法都一般,无法根治这个问题,我也是买之前没做足功课,入手了丐版 8+256 Macbook,日常就是用用 Edge,IDEA,Pycharm,不到一年时间固态写了将近40T。

只能自己没事多留心,别同时开太多吃内存的软件,用不到的软件就 Command+Q 。

外接固态

外接一个固态硬盘,下载的东西都直接存入固态中,但对正常用户来说,这并不是磁盘写入量的罪魁祸首。

减小 WindowServer 内存占用

占用过多mac内存的WindowServer是什么

关闭虚拟内存

即使关闭了虚拟内存,也不会影响系统稳定性,因为超过阈值又会继续使用虚拟内存,其实效果并不是很好,开个 IDEA 内存就飚起来了。但关闭后就可以在没必要的情况下不去使用虚拟内存。

M1关闭虚拟内存(交换内存 swap memory) 防止SSD磨损

关闭 Spotlight 索引

https://www.v2ex.com/t/874851

但直接关闭索引对正常使用太影响了,比较折中的办法是设置一个 crontab 定时任务,每天允许 Spotlight 索引一个小时。

Disable spotlight indexing on macOS to heavily speed up Virtual Instances.
# massively increase virtualized macOS by disabling spotlight.
sudo mdutil -i off -a

# since you can't use spotlight to find apps, you can renable with
# sudo mdutil -i on -a
相关推荐
黑果魏叔3 小时前
macOS Sequoia 正式版(24A335)黑苹果/Mac/虚拟机系统镜像
macos
tekin4 小时前
macos macport软件包管理工具 sudo port install xxx 安装的软件的路径 与 brew install xxx 软件安装路径总结
macos·brew·port·macport·port install·port软件包安装路径·brew软件包安装路径
Hellc0076 小时前
MacOS升级ruby版本
前端·macos·ruby
GEEKVIP13 小时前
Android 恢复挑战和解决方案:如何从 Android 设备恢复删除的文件
android·笔记·安全·macos·智能手机·电脑·笔记本电脑
逢生博客18 小时前
Mac 搭建仓颉语言开发环境(Cangjie SDK)
macos·华为·鸿蒙
Rverdoser21 小时前
MacOS Catalina 从源码构建Qt6.2开发库之01: 编译Qt6.2源代码
macos
GEEKVIP1 天前
如何在没有备份的情况下恢复 Mac 上丢失的数据
经验分享·笔记·安全·macos·电脑·笔记本电脑·改行学it
yanling20231 天前
黑神话悟空mac可以玩吗
macos·ios·crossove·crossove24
Vzhangs1 天前
MacOS Sonoma(14.x) 大写模式或中文输入法下的英文模式,光标下方永远会出现的CapsLock箭头Icon的去除办法
macos
2401_852403551 天前
Mac导入iPhone的照片怎么删除?快速方法讲解
macos·ios·iphone