在 Ubuntu(或其他 Linux 系统)上“优化进程”通常可以从系统层面、进程层面、资源调度、内核参数几个角度来做。下面按实用程度分类说明。
top
htop # 推荐,需安装:sudo apt install htop
ps aux
free -h # 内存
df -h # 磁盘
iostat -x 1 # IO(需 sysstat)
vmstat 1 # 综合
nice -n 10 ./app
renice 10 -p PID
# 限制 CPU 核数
taskset -c 0,1 ./app
# 限制资源(cgroup)
systemd-run --scope -p CPUQuota=50% -p MemoryMax=512M ./app
nohup ./app > app.log 2>&1 &
# 或
screen / tmux
systemctl list-unit-files --type=service
sudo systemctl disable bluetooth
sudo systemctl stop cups
sudo systemctl edit nginx
添加:
[Service]
CPUQuota=80%
MemoryMax=1G
ulimit -n 65535
永久:
/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
/etc/sysctl.conf
示例:
vm.swappiness=10
net.core.somaxconn=65535
fs.file-max=1000000
生效:
sysctl -p
cat /sys/block/sda/queue/scheduler
echo mq-deadline > /sys/block/sda/queue/scheduler
taskset -c 2-7 ./server
swappinesszramsudo apt install zram-config
cgroupsdocker run --cpus=2 --memory=1g image
❌ 盲目杀进程
❌ 过度调内核参数
❌ 忽略 IO 瓶颈(比 CPU 更常见)
如果你能告诉我:
我可以给你针对性的优化方案。