Memory

How to see memory usage of a service with systemctl?

The systemctl command can be used to show the memory usage of a service managed by systemd.

Summary

The systemctl command has multiple options to show the memory usage. With the status subcommand followed by the service, it will show the basics, including memory usage.

To retrieve the information that easier to parse, then use show followed by --property=MemoryCurrent and the service name.

Usage

The status output will include memory usage.

systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/nginx.service.d
             └─override.conf
     Active: active (running) since Mon 2024-06-17 17:59:45 UTC; 3h 54min ago
       Docs: man:nginx(8)
   Main PID: 36971 (nginx)
      Tasks: 2 (limit: 1012)
     Memory: 2.6M
        CPU: 26ms
     CGroup: /system.slice/nginx.service
             ├─36971 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             └─36972 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

For pulling in the information that can be parsed or scripted, consider retrieving the actual property from a running service.

Smem

The command smem can help showing memory usage, including the usage of swap.

Summary

Pidstat

The command pidstat provides details about CPU, memory, and disk activity by processes.

Summary

Memory

Articles and information about how memory, such as RAM, is being used on Linux systems. Great for system administration and troubleshooting purposes.

Summary

Vmstat

The command vmstat reports information about memory, paging, processes, disks, block IO, CPU activitiy, and traps.

Summary

Swap memory information

Articles and tools to troubleshoot Linux system performance issues with focus on swap memory and its usage.

Summary

Physical RAM is used to store information. Linux divides this RAM into smaller chunks, named memory pages. When there is no more normal memory available, the Linux kernel might need to temporarily store information aside. This is called paging or swap space.

During the process of paging, memory pages will be moved from the RAM to the disk. This way memory is freed up for active processes, while older information is temporarily stored on the disk. A disk is much slower than RAM, so typically you want to avoid paging as much as possible.

Dmidecode cheat sheet

Want to see all hardware details of a system? Then dmidecode is your friend, helping to decode all information from the SMBIOS specification.

Summary

All hardware exposed

Understanding memory information on Linux systems

Linux memory management is an extensive subject. This guide helps you understanding the how to analyze it and obtain available memory information.

Summary

Every operating system needs memory to store program code segments and data. This is also true for Linux systems. The problem: there is a lot of information available regarding memory usage and its behavior. Let’s discover how Linux manages its memory and how we can gather memory information.

After reading this guide, you will be able to:

  • Show the total amount of memory
  • Display all memory details
  • Understand the details listed in /proc/meminfo
  • Use tools like dmesg, dmidecode, free, and vmstat

Linux memory information

Random access memory

When we talk about memory in this article, we usually mean random access memory (RAM). This is the memory which can be used for both showing and storing data. Typically we will find in this type of memory the programs that are running on the system, including the Linux kernel itself. Besides the program code, memory also stores a lot of data. A good example is when you are running a MySQL database server. The program itself is relatively small, the data itself is huge. So we will also have a look at tuning programs and their memory usage, as this is typically a problem with memory-hungry programs.

Linux and ASLR: kernel/randomize_va_space

ASLR protects the Linux kernel and programs against different attacks. It can be tuned with the randomize_va_space setting to provide different protections.

Summary

Configuring ASLR with randomize_va_space

The Linux kernel has a defense mechanism named address space layout randomization (ASLR). This setting is tunable with the randomize_va_space setting. Before making changes to this setting, it is good to understand what this Linux security measure actually does and how it works.

Understanding ASLR

In 2001 the term ASLR was first introduced as a patch to the Linux kernel. Its main goal was to randomize memory segments to make abuse by malicious programs harder. A normal program consists of several components, which are loaded into memory and flagged with special properties. Some pieces of the program are executable bits, others are normal data. Before going into these properties, let’s first determine the main goal of a program. Simply said, it should have a start procedure, maintain itself, and finally end. For some programs this whole cycle can take milliseconds, others may take years to complete. It all depends on the program, its stability and how often a system is rebooted.

Linux Security Principle: Containment of Failure

Everyone who ever used Windows 95, is familiar with the concept of failure. Fortunately, Linux systems have a strong foundation and use containment of failure.

Summary

Everyone who used Windows 95 or 98 in the past is familiar with the concept of failure. One crashing application was enough to bring the system to a halt. Fortunately, Linux systems have a strong foundation, including privilege separation and memory management. When things go wrong, the impact is reduced to a minimum. This is called containment.

Linux Memory Management

Memory is like your the storage capacity of your brain. Every bit should be stored properly, or otherwise you will do strange things. Linux systems have powerful memory management, to ensure that data is properly sorted and permissions are assigned. For example an ELF binary, the most common binary format on Linux, has different sections for executable code and data. Then on top of that, each section gets different permissions in memory. For example code could be marked as read-only, to prevent it being overwritten by itself or another process.