Monitoring Memory Usage In Embedded Linux: Practical Tips

how to monitor memory usage embedded linux

Monitoring memory usage is a critical aspect of managing and maintaining embedded Linux systems, especially as many manufacturers are now connecting their products to the internet, turning them into Internet of Things (IoT) devices. While there are numerous methods and tools available, one simple way to check memory usage is by using the 'free -m' command, which will display the remaining RAM size available. Another option is to use 'cat /proc/meminfo' to access detailed memory statistics, including total RAM, free memory, buffers, and more. Additionally, tools like 'top' or 'htop' can provide insights into memory usage on a process-by-process basis, helping to identify any potential memory leaks or performance issues.

Characteristics Values
Memory monitoring tool top, htop, free -m, cat /proc/meminfo, ps, busybox's top, readelf, Codesighs
RAM usage monitoring Yes
CPU usage monitoring Yes
Disk usage monitoring Yes
Application errors monitoring Yes

shundigital

'cat /proc/meminfo'

The /proc/meminfo file inside the /proc pseudo-filesystem provides a usage report about memory on the system. To see what a file contains, we can use the cat command:

$ cat /proc/meminfo

MemTotal: 994548 kB

MemFree: 65228 kB

MemAvailable: 263724 kB

Buffers: 21396 kB

Cached: 304440 kB

SwapCached: 25260 kB

Active: 267424 kB

Inactive: 503720 kB

Active(anon): 110956 kB

Inactive(anon): 351176 kB

The /proc/meminfo file contains memory statistics in kilobytes. The basic information we can get from this file includes:

  • MemTotal: total usable RAM
  • MemFree: free RAM, the memory which is not used for anything at all
  • MemAvailable: available RAM, the amount of memory available for allocation to any process

We can also use the cat command to get more detailed information about our memory usage. For example, to check the buffer and cache sizes, we can use the following command:

$ cat /proc/meminfo | grep -e "Buffers" -we "Cached"

Buffers: 12820 kB

Cached: 254592 kB

Buffers refer to temporary storage components for process input and output, while cache stores data to serve future requests faster, increasing processing speed.

We can also use cat /proc/meminfo to learn about the swap space on the system:

$ cat /proc/meminfo | grep "Swap"

SwapCached: 13936 kB

SwapTotal: 945416 kB

SwapFree: 851064 kB

Swap space is a backup space on the disk for RAM. When the physical memory is full, the system uses the swap space.

In addition, the /proc/meminfo file also contains data about memory dynamics, such as active and inactive memory:

$ cat /proc/meminfo | grep -e "Active" -e "Inactive"

Active: 194308 kB

Inactive: 553172 kB

Active memory has been used more recently and is not suitable for reclamation for new applications. Inactive memory, on the other hand, hasn't been used recently and can be reclaimed for new applications.

shundigital

'free -m' command

The `free` command is a useful tool for checking memory usage in an embedded Linux system. The command provides a summary of RAM usage, including total, used, free, shared, and available memory, as well as swap space.

The `free` command is invoked with the -m option, which displays the output in a human-readable format, with memory amounts measured in megabytes.

Total used free shared buffers cached

Mem: 255508 240268 15240 0 7592 86188

/+ buffers/cache: 146488 109020

Swap: 530136 26268 503868

In this output, the `Mem` row displays physical memory utilisation, while the `Swap` row shows the utilisation of the system swap space. The `-/+ buffers/cache` row indicates the amount of physical memory devoted to system buffers.

The `free` command is useful for short-term monitoring or for quickly identifying memory-related issues. However, for long-term monitoring, the output can be challenging to interpret as it only displays memory utilisation information once, and the output scrolls, making it difficult to track changes over time.

To overcome this limitation, the `watch` command can be used in conjunction with `free -m` to continuously update the memory utilisation information on the same screen, making it easier to observe changes. For example, the command `watch -n 1 -d free -m` will update the memory utilisation every second and highlight any changes.

shundigital

'top' utility

The top utility is a useful tool for monitoring memory usage in an embedded Linux system. It provides a dynamic view of the system's memory allocation and usage, allowing users to identify potential issues and optimize their system's performance.

When running the top command, you will be presented with a comprehensive overview of the system's memory statistics. The output includes various metrics such as the total amount of physical RAM (MemTotal), the amount of free physical RAM (MemFree), buffer memory usage (Buffers), and cache memory usage (Cached). These statistics provide valuable insights into how the system is utilizing its memory resources.

In addition to memory usage, top also displays CPU usage information. This includes the percentage of CPU time used by each process (%CPU) and the total CPU utilization. This feature helps users identify processes that are consuming a significant amount of CPU resources and may require optimization.

Another advantage of the top utility is its ability to provide process-specific memory usage. By default, top displays the virtual memory size (VSZ) used by each process. However, as mentioned in the search results, top can also show the physical RAM used by each process (RES). This is particularly useful for embedded systems, as it allows developers to pinpoint memory-intensive processes and optimize their memory allocation.

Furthermore, top offers additional insights through its interactive mode. By pressing the "i" key while top is running, you can access more detailed information about each process, including memory mappings and CPU usage statistics. This interactive mode enhances the utility's functionality, making it a versatile tool for memory and performance monitoring.

Overall, the top utility is a powerful and versatile tool for monitoring memory usage in embedded Linux systems. Its ability to provide real-time statistics, process-specific memory usage, and interactive mode makes it a go-to choice for developers and system administrators seeking to optimize their system's performance and memory allocation.

shundigital

'ps' command

The 'ps' command is a versatile tool for displaying information about active processes and their memory usage. It provides basic memory usage information, such as RSS (Resident Set Size), but does not account for shared memory.

  • `ps -e -orss=,pid=,args= | sort -b -k1,1n: This command lists processes by memory usage, displaying the resident set size (RSS) in kilobytes, the process ID (PID), and the command-line arguments for each process. The output is then sorted by RSS in ascending order.
  • `ps -e -o pid,user,%mem,cmd --sort=-%mem`: This command shows the memory usage of all processes on the system, sorted by memory usage in descending order. The output includes the process ID, user, percentage of memory used, and the command.
  • `ps -eo pid,ppid,cmd,pmem,rss --no-headers --sort=-rss | awk '{if ($2 ~ /^[0-9]+$ / && $6/1024 >= 1) {printf "PID: %s, PPID: %s, Memory consumed (RSS): %.2f MB, Command: ", $2, $3, $6/1024; for (i=4; i<=NF; i++) printf "%s ",$i; printf "\n"}}': This command calculates the memory usage in a human-readable format, such as megabytes. It filters out processes with very low memory usage and converts the RSS value from kilobytes to megabytes.
  • `ps -eo user,pid,ppid,cmd,pmem,rss --no-headers --sort=-rss | awk '{if ($2 ~ /^[0-9]+$ / && $6/1024 >= 1) {printf "PID: %s, PPID: %s, Memory consumed (RSS): %.2f MB, Command: ", $2, $3, $6/1024; for (i=4; i<=NF; i++) printf "%s ",$i; printf "\n"}}': This command is similar to the previous one, but it includes the username in the output.
  • `ps -eo pid,ppid,cmd,pmem,rss --no-headers --sort=-rss: This command displays the process ID, parent process ID, command, percentage of memory used, and resident set size (RSS) for each process, sorted by RSS in descending order.

shundigital

'htop' utility

The htop utility is a command-line tool that allows users to monitor their system's vital resources and server processes in real time. It is an interactive process viewer that provides detailed information about CPU and memory usage, load average, uptime, and individual process statistics such as priority, CPU and memory consumption.

To use htop, you need to connect to your server via an SSH connection. Once connected, simply type "htop" and press Enter. The htop window provides a wealth of information, including CPU and memory usage at the top left, load average and uptime at the top right, and real-time data of processes with stats like priority, CPU usage, and memory consumption.

The CPU line shows the percentage of CPU being used, with colour coding indicating the type of process: red for kernel processes, green for normal user processes, and blue for low-priority processes. The memory consumption is also colour-coded, with green indicating used memory pages, blue for buffer pages, and yellow for cache pages.

The load average section provides three values: a one-minute average, a five-minute average, and a fifteen-minute average. Below this, you will find detailed information about each process running on your server, including the process ID (PID), process owner (USER), virtual memory consumption (VIRT), percentage of CPU usage (%CPU), percentage of physical RAM used (%MEM), and the command that started the process (COMMAND).

Htop also offers a range of commands that allow you to perform various functions, such as sorting processes by CPU usage or memory usage (F6). It is a powerful tool for troubleshooting and killing processes that are utilising excessive server resources.

Frequently asked questions

You can use commands like "free -m" or "cat /proc/meminfo" to check memory usage.

Tools like "top", "htop", and "ps" can be used to monitor memory usage.

You can use "cat /proc//maps" or "ps" with various options to check memory usage for a specific application.

You can use commands like "top" or "free" and then use "grep" to dump the output to a log file.

Monitoring RAM usage over time is crucial to identify memory leak issues. Receiving instant alerts on issues and saving time and costs on support are also important considerations.

Written by
Reviewed by
Share this post
Print
Did this article help you?

Leave a comment