Junior — Middle
How can the amount of free RAM in the system be determined?
sobes.tech AI
Answer from AI
Determining the amount of free RAM in a system can be done in various ways depending on the operating system and tools:
-
Linux:
- The command
free -mshows free and used memory. - Reading from
/proc/meminfoprovides detailed information. - Using utilities like
vmstat,top,htop.
- The command
-
Windows:
- The command
systeminfoorwmic OS get FreePhysicalMemory. - Using PowerShell:
Get-CimInstance Win32_OperatingSystem | Select-Object FreePhysicalMemory.
- The command
-
macOS:
- The command
vm_stat. toporsysctl hw.memsize.
- The command
Example for Linux in bash:
free -m | awk 'NR==2{printf "Free memory: %s MB\n", $7}'
For programmatic access in Go, you can use third-party libraries like github.com/shirou/gopsutil/mem:
import (
"fmt"
"github.com/shirou/gopsutil/mem"
)
func main() {
v, _ := mem.VirtualMemory()
fmt.Printf("Free memory: %v MB\n", v.Available/1024/1024)
}
Thus, the choice of method depends on the context and platform.