Super User Asked by Rocketman173 on December 27, 2021
I’m running an apache2.4 web server, and noticed that when I run systemctl status apache2
or run service apache2 status
, it says it is using 4.2GiB of RAM, which isn’t a problem for the amount of RAM I have, but I am confused, since top
and htop
both show only 500MiB of RAM being used, much less that systemctl
reports apache2 using alone. Why might this be, and which is right?
OS Info, if needed:
OS: Kali GNU/Linux Rolling x86_64
Kernel: 4.19.0-kali3-amd64
The tl;dr story is that systemctl
more thoroughly accounts for the memory a process is using, and reports on the total memory usage of an entire service, while top
gives a more lenient accounting of the memory used by a single process. For a service like apache2
which runs several processes, systemctl
can be expected to show much higher memory usage than top
.
Memory management in *nix is extremely complex because RAM is the limiting resource for most operations, so a lot of work has gone into making the most use of it. As such, there are different categories of memory or memory usage. I was going to list them all, but it gets kind of insane pretty quickly.
I can list some of the major categories, though. The number one distinction is RAM versus Disk. "Memory" just means a place to store data. In a lightly loaded system, all data a program needs, including the program code itself, is in RAM. As more programs want access to more data, "memory" expands to include data stored on disk. Moving "memory" between RAM and Disk is highly optimized (and complex), but since the kernel can do it, it does it in a lot of ways.
So, here is a simplified, incomplete list of categories of memory usage
On top of those categories, we also have:
So when you ask "how much memory is this program using?" there is no one answer. There really is not any good answer to such a vague question. (See this article for 6 different definitions of the question along with some discussion about why they are hard to answer.) So different programs give you different answers, often with labels attached.
Programs like top
try to answer the question with something like "how much RAM would be freed up if I quit this program". The Linux top
man page provides a lot of detail about how it categorized memory and what it displays.
On the other hand systemctl
tries to account for all the memory a service
is using, both in RAM and Swap and to some extent shared and file backed, in order to be able to automate limits on resource usage and go as far as killing a program if it uses too much memory. It uses cgroups
to provide much more extensive accounting for the memory a process uses, and that is described in great detail in section 2.2 "Accounting" of this article.
So even if the service is only running one process, because systemctl
includes in the total memory usage of a process any shared pages it loaded first (before any other process), any Swap it is using, any memory cache (such as file system or network buffers) it is using, and probably some other stuff, it can report a higher memory usage than top
. When a service is running more than once process, then obviously you would expect memory usage to go up even further.
Answered by Old Pro on December 27, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP