Sobes.tech
Middle+

How to check CPU and memory load in Linux?

sobes.tech AI

Answer from AI

To check CPU and memory load in Linux, you can use several standard tools:

  • top — an interactive utility showing current CPU load, memory usage, processes, and other information.
  • htop — an improved version of top with a user-friendly interface.
  • vmstat — displays statistics on memory, processes, input/output, and CPU.
  • free — shows the amount of free and used memory.
  • cat /proc/loadavg — shows the average CPU load over 1, 5, and 15 minutes.

Example of using the top command:

top

For programmatic checking in Go, you can read data from /proc/stat for CPU and /proc/meminfo for memory, or use third-party libraries such as github.com/shirou/gopsutil:

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/cpu"
    "github.com/shirou/gopsutil/mem"
)

func main() {
    cpuPercent, _ := cpu.Percent(0, false)
    vmStat, _ := mem.VirtualMemory()

    fmt.Printf("CPU Usage: %.2f%%\n", cpuPercent[0])
    fmt.Printf("Memory Usage: %.2f%%\n", vmStat.UsedPercent)
}
How to check CPU and memory load in Linux? — Golang - sobes.tech