How to monitor and manage your Raspberry Pi’s memory usage using command line tools.
Learn how to check Raspberry Pi memory usage, detect memory leaks, and optimize memory consumption with command line tools. …
Updated September 5, 2023
Learn how to check Raspberry Pi memory usage, detect memory leaks, and optimize memory consumption with command line tools.
Raspberry Pis are popular for their low power consumption and ability to run many different applications. However, managing the resources on these small devices can be challenging. One of the most important resources to monitor is memory usage, as it can affect system performance and stability. In this article, we’ll cover how to check Raspberry Pi memory using command line tools such as free
, top
, and htop
. We’ll also discuss ways to detect memory leaks and optimize memory consumption.
Checking Memory Usage with free
The most basic tool for checking memory usage is the free
command. This command displays information about the system’s total, used, and available physical and swap memory. To use it, simply open a terminal window on your Raspberry Pi and type:
free -h
The -h
option shows the output in a human-readable format (e.g., using KB, MB, or GB). The output will look something like this:
total used free shared buff/cache available
Mem: 948M 217M 36M 504K 694M 666M
Swap: 999M 4.0M 995M
In this example, we have 948MB of physical memory and 999MB of swap space. Our system is using 217MB of memory, with 36MB free and 694MB in buffers/cache. The available
column shows how much memory is available for new processes to use (without swapping).
Checking Memory Usage with top
The top
command provides a dynamic view of running processes, sorted by resource usage. This can be helpful for identifying memory hogs and other performance issues. To run it, simply type:
top
Press Shift+M
to sort the processes by memory usage. The top line of output will show overall system statistics, followed by a list of running processes:
top - 18:42:35 up 7 days, 5:09, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 1162 total, 1 running, 161 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.1 sy, 0.0 ni, 99.4 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st
MiB Mem : 948.6 total, 37.5 free, 208.1 used, 693.0 buff/cache
MiB Swap: 999.0 total, 14.5 free, 984.5 used. 207.7 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3162 pi 20 0 395484 68780 46340 S 0.0 7.2 0:00.17 python
1772 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/u8:1
3169 pi 20 0 52240 3240 2732 R 0.0 0.3 0:00.01 top
1 root 20 0 186320 6704 4944 S 0.0 0.7 0:01.57 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.05 ksoftirqd/0
The RES
column shows the resident memory size for each process (in bytes). You can see that the Python process is using 68780 KB of memory, while other processes are using much less.
Checking Memory Usage with htop
htop
is an interactive version of top
, providing a more user-friendly interface and additional features like colors and scrolling. To install it, run:
sudo apt update && sudo apt install htop
Once installed, you can run it with:
htop
Press F5
to sort processes by memory usage, or use the mouse wheel to scroll through the list. The interface is self-explanatory and provides a real-time view of your Raspberry Pi’s resources.
Detecting Memory Leaks
A memory leak occurs when a program continuously allocates memory but does not release it, causing system performance to degrade over time. To detect leaks, you can use tools like valgrind
or monitor the change in memory usage over time with free
.
For example, run free -m
every few seconds and note any significant increases:
while true; do echo -n "$(date) "; free -m | awk '/Mem:/ {print $3 "/" $2 " MB"}'; sleep 5; done
This will print the used and total memory in MB, updating every 5 seconds. If you notice a consistent increase, it may indicate a memory leak.
Optimizing Memory Consumption
There are several ways to optimize memory consumption on your Raspberry Pi:
- Use lightweight alternatives: Switch to lighter-weight alternatives for common programs, such as using
lite
versions of software or using text-based tools instead of graphical ones. - Limit background services: Disable unnecessary system services and daemons to free up memory.
- Close unused applications: Make sure to close programs you’re not using, especially if they’re resource-intensive.
- Use swap space: If physical memory is low but swap space is available, the system will use it to store less frequently used data. However, this can slow down your device.
- Adjust process priorities: Use
nice
andionice
commands to lower the CPU and I/O priority of certain processes, allowing them to run only when there’s free memory.
Remember that memory optimization is an ongoing process. Monitoring your Raspberry Pi’s memory usage with the tools mentioned above will help you identify potential issues and make informed decisions about how best to manage resources.