متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 11System · Linux

How to check memory and CPU usage

"The server is slow." Before you restart anything, find out what is busy: the CPU, the memory, the disk, or one process. This how-to gives the quick commands, explains the numbers that confuse most people (load average, "used" memory, swap), shows how to find the top process and the service behind it, and how to see if the kernel killed something because memory was full (OOM).

  • 5 الدروس
  • 16 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

11.1

The quick answer

uptime
 10:42:07 up 12 days,  3:10,  2 users,  load average: 3.92, 2.10, 1.05
nproc
4
free -h
               total        used        free      shared  buff/cache   available
Mem:           7.7Gi       5.9Gi       210Mi        92Mi       1.9Gi       1.5Gi
Swap:          2.0Gi       1.1Gi       900Mi
ps -eo pid,user,%cpu,%mem,rss,cmd --sort=-%cpu | head -6
ps -eo pid,user,%cpu,%mem,rss,cmd --sort=-%mem | head -6
top -o %MEM

Read it like this: load average compared with nproc (4 cores: load 4 = full); memory: look at available, not "free".

11.2

Step by step

  1. Is the CPU full? The three load numbers are the last 1, 5 and 15 minutes. Load higher than the number of cores means work is waiting. In top, the %Cpu(s) line tells you why:
Field in topMeaningHigh value means
usUser programsAn app is busy (look at the top process).
syKernelMany system calls, network or disk work.
waWaiting for disk I/OThe disk is slow, not the CPU.
stStolen by the hypervisorThe VM host is overloaded (cloud: bigger or other instance).
idIdleFree CPU.
  1. Use top well. Keys inside top: P = sort by CPU, M = sort by memory, 1 = one line per core, c = full command, k = kill, q = quit. One snapshot for a ticket:
top -b -n 1 -o %CPU | head -20
  1. Is the memory full? On Linux "free" is almost always small — the kernel uses spare RAM as cache (buff/cache) and gives it back when needed. The real number is available. Swap that is used is fine; swap that is moving all the time is a problem:
vmstat 2 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 5  1 1153024 214000  51200 1890000  420  610   980  1200 2100 3900 62 11  9 18  0

si/so (swap in/out) above 0 again and again = the server needs more RAM or a smaller app. r = processes waiting for CPU, b = blocked on I/O.

  1. Find the service behind a process. A PID alone is not enough — find its systemd unit or container:
ps -o pid,ppid,user,etime,cmd -p 2817
systemctl status 2817
systemd-cgtop -m

systemctl status PID shows the service of that process; systemd-cgtop shows CPU and memory per service (and per container).

  1. Did the kernel kill a process because RAM was full? Look for the OOM killer:
journalctl -k --since "-1 day" | grep -iE 'out of memory|oom-kill'
Nov 17 03:12:44 web1 kernel: Out of memory: Killed process 2817 (java) total-vm:9123456kB, anon-rss:6012345kB
  1. Watch over time (the problem is often not there when you look):
watch -n 2 'free -h; uptime'
cat /proc/pressure/cpu /proc/pressure/memory

The pressure files (PSI) show how much time tasks waited for CPU or memory — avg10=0.00 is good, high values mean real waiting.

11.3

Check that it worked

After you fixed the cause (stopped a runaway job, gave the app a memory limit, added RAM): the load falls below the core count within some minutes, available memory stays well above zero, vmstat shows si/so at 0, and no new OOM lines appear in journalctl -k.

11.4

Common problems

ProblemCauseFix
"free" is only 200 MB — is the server out of RAM?No: the kernel uses free RAM as cache.Look at available. Only a small available is a problem.
High load but CPU mostly idleProcesses wait for the disk (wa, state D).Check the disk: iostat -x 2 (package sysstat), df -h, slow NFS/SAN.
A process uses more than 100 % CPUIt uses more than one core (100 % = one core).Normal for multi-threaded apps; compare with nproc.
An app dies at night, no error in its logThe OOM killer stopped it.journalctl -k, then give it more RAM or limit it (MemoryMax= in the unit, container --memory).
st (steal) is high on a cloud VMThe host or a burstable instance (CPU credits) is out of CPU.Move to another instance type or size.
Numbers in a container look wrongfree/top inside a container show the host.Use docker stats or kubectl top pod.

11.5

Go further

1A 4-core server shows a load average of 8. What does that mean?

About twice as much work as the CPUs can do — tasks are waiting (for CPU or for the disk; check wa in top).

2Which memory number shows how much RAM apps can still use?

available in free -h — not free.

كل الأوامر في هذا الفصل