Matt pointed out that the currently-available information is confusing at
best. The page cache muddies the situation, and the sharing of pages
between applications complicates things even more. The result is that it
is hard to say where memory is being used; one can't even get a definitive
answer to the question of how big a specific application is. More detailed
questions - such as which parts of an application are using the most memory
- are even harder to answer. Trying to answer questions of interest to
embedded systems developers - how many applications can run on a specific
device without pushing it into thrashing, for example - is nearly
impossible without simply running a test.
The problem is that the numbers exported by the current kernels are nearly meaningless. The reported virtual size of an application is nearly irrelevant; it says nothing about how much of that virtual space is actually being used. The resident set size (RSS) number is a little better, but there is no information on sharing of pages there. The /proc/pid/smaps file gives a bit of detail, but also lacks sharing information. And the presence of memory pressure can change the situation significantly.
The Linux virtual memory system, in other words, is a black box which provides too little information on what is going on inside. Matt's project is to open up that box and shine some light inside.
The first step is to add a new file (pagemap) in each process's /proc directory. It is a binary file containing the page frame number for each page in the process's address space. The file can be read to see where a process's pages have been placed and, more interestingly, it can be compared between processes to see which pages are being shared. Matt has a little graphical tool which can display this file, showing the patterns of which pages are present in memory and which are not.
Then, there is a file (/proc/kpagemap) which provides information about the kernel's memory map. For each physical page in the system, kpagemap contains the mapping count and the page flags. This information can be used to learn about sharing of pages and about how each page is being used. There were a couple of graphical applications using this file as well; one showed the degree to which each page is being shared, while the other showed the use of each page as determined by its flags.
Once this information is available, one can start to generate some useful numbers on memory use. Matt is proposing two new metrics. The "proportional set size" (PSS) of a process is the count of pages it has in memory, where each page is divided by the number of processes sharing it. So if a process has 1000 pages all to itself, and 1000 shared with one other process, its PSS will be 1500. The unique set size (USS), instead, is a simple count of unshared pages. It is, for all practical purposes, the number of pages which will be returned to the system if the process is killed.
These numbers are relatively expensive to calculate, since they required a pass through the process's address space. So they will not be something which is regularly exported from the kernel. They can be calculated in user space using the pagemap files, though. Matt demonstrated a couple of tools to do these calculations. Using "memstats" on a galeon process, he supplemented the currently-available virtual size and resident set size numbers (105MB and 41MB, respectively) with a PSS of 26MB and a USS of 20MB. There is also a "memrank" tool which lists processes in the system sorted by decreasing PSS. With a tool like that, finding the memory hogs on the system becomes a trivial task.
Matt pointed out that these numbers, while useful, will change depending on the amount of memory pressure being experienced by the system. It would be nice to be able to figure out how much memory a given process truly needs before it will begin to thrash. To this end, his patch creates a new clear_refs file for each process; this file can be used to reset the "referenced" flag on each page in the process's working set. After the process runs for a bit, one can look at which pages have had their referenced bits set again; those are the pages it actually needed to run during that time.
The patches are in the -mm tree currently; it's possible that they could find their way into the mainline once the 2.6.22 merge window opens up. Those who would like to play with Matt's scripts can find them in this directory; the slides from his talk are packaged there as well. With luck, understanding system memory usage will require far less guesswork in the near future.
Wow!
Posted Apr 19, 2007 14:58 UTC (Thu) by AnswerGuy (subscriber, #1256) [Link]
Having written an internal guide to "Understanding Memory Utilization" for
Wow!
I'm so looking forward to having this incorporate into the mainstream. I
can scarcely imagine how much easier it will be to administer systems and
monitor memory capacity issues with these.
(Of course we'll still strive for a simple rule of thumb ... if you don't
have about half of your RAM available for caching ... you could probably
use a bit more in the system). :)
(I'm also looking forward to seeing the merge of I/O stats and atop compatible kernel patches that allow us to find the I/O hogs on a per process and per channel basis; that's been a, by far, the worst gap in
the performance analysis arsenal for Linux).
JimD
Seconded
Posted Apr 20, 2007 7:05 UTC (Fri) by dion (guest, #2764) [Link]
I've always been somewhat confused that the default kernel couldn't provide the data atop needs to display IO stats, as that's one of the most useful things I've ever seen for tuning a Linux system.
I'm sorry to be somewhat redundant here, but I'd like to say a big "me too" on atop and memstats.
Wow!
Posted Apr 20, 2007 18:10 UTC (Fri) by nlucas (subscriber, #33793) [Link]
+1Looking forward for this to be integrated.
Wow!
Posted Apr 24, 2007 12:30 UTC (Tue) by jospoortvliet (subscriber, #33164) [Link]
Also a +1 from me. This information is hard to come by, and it's really wonderful we might see it available in the kernel by default.
Wow!
Posted Oct 26, 2007 22:28 UTC (Fri) by MarkSeger (guest, #35422) [Link]
Clearly this will all be a good thing and even though the memory stats aren't perfect, you CAN look at memory and any other stats you want with the tool I build called collectl, which you can get at http://collectl.sourceforge.net/ Collectl allows to to gather just about any of the major system performance metrics and display then side-by-side or in more deal on multiple lines. There's even a format that allows you to put it in a format understandable by gnuplot. Here's a simple example of just looking at memory and disk #<-----------Memory----------><-----------Disks-----------> #free buff cach inac slab map KBRead Reads KBWrit Writes 55M 552M 2G 532M 0 0 0 0 0 0 55M 552M 2G 532M 0 0 0 0 220 6 55M 552M 2G 532M 0 0 0 0 0 0 but there are far too many combinations to even try. Check it out and see what you think. btw - I chose to leave off time in the output above to save screen real estate but you can easily add it in with a simple switch and if you want even more details, can include msec! Why is this important? Because collectl can run at sub-second intervals and if you care about network stats and don't monitor at an interval of 0.9765 you'll lose accuracy. If you don't think that's true, I have a page on the website that goes into the details. -mark
Wow!
Posted Oct 27, 2007 0:10 UTC (Sat) by nix (subscriber, #2304) [Link]
What a scarily useful-looking tool. Thank *you*.
ELC: How much memory are applications really using?
Posted Apr 24, 2007 16:05 UTC (Tue) by richdawe (subscriber, #33805) [Link]
There's also a tool called exmap <http://www.berthels.co.uk/exmap/>, which does something similar, although perhaps not as comprehensively. There's also a console version for embedded applications, called exmap-console <http://projects.o-hand.com/exmap-console> -- there was an interesting presentation at FOSDEM on it.
I like the Proportional Set Size and Unique Set Size measurements mentioned in the article -- hopefully they will be exposed in top.
ELC: How much memory are applications really using?
Posted Jun 29, 2007 12:10 UTC (Fri) by pixelbeat (guest, #7440) [Link]
Also have a look at the ps_mem.py tool
ELC: How much memory are applications really using?
Posted Sep 20, 2007 16:06 UTC (Thu) by pixelbeat (guest, #7440) [Link]
I've just updated ps_mem.py to use PSS when available:
ELC: How much memory are applications really using?
Posted Mar 3, 2015 20:56 UTC (Tue) by rael (guest, #40124) [Link]
https://github.com/crquan/coremem
Improvements over pixelb's python script:
1) written with Go language's concurrent model that makes it run much faster, on a server with 1400 processes running this takes 2s to print results, vs. the ps_mem.py takes 36s; Go compiler's default output is a static binary, makes it useful with hosts where there is no python.
2) ignoring access permission error if run with normal user id, this is useful for desktop users, they can get core mem information for their own processes, while administrator can still run with sudo for the whole system.
ELC: How much memory are applications really using?
Posted May 31, 2007 18:31 UTC (Thu) by czr (guest, #13701) [Link]
It is somewhat funny how nowadays many people working with Linux seem to
Some time ago I wrote a tool that does reporting based on URES (unique
residest size, RRES - SHARED in this case, which is the same as USS). For
this to work, no kernel patching is required. And of course the number by
itself has the same limitations as the USS.
The tool (written in Python) can be found here:
http://koltsoff.com/pub/meminfo . And another page explaining URES is
mentioned there as well.
The idea of URES/USS has been implemented (at least in the VCS-version)
of KDE ksysguard and based on short email communication will not be
implemented in gnome-system-monitor.
When dealing with systems that are running X server(s), it also is useful
to know that GUI programs often use bitmaps (and other resources) that
are actually accounted in X server at some point, and this will lead to
somewhat skewed memory usage results. This will also apply to any
client-server system in which part of client resources are allocated (and
present in memory of) the server.
ELC: How much memory are applications really using?
Posted Jan 5, 2009 8:56 UTC (Mon) by Blaisorblade (guest, #25465) [Link]
xrestop can give some stats about those resources. However, I used to have the gut feeling that the increase in memory usage of the X server caused by client programs is bigger than the one reported by xrestop.
For instance, right now ~130M (mostly for pixmaps, with 40M for Firefox with ~60 tabs in 4 windows) are allocated by client programs on my system, out of 300M of X's RSS, and 390M of anonymous data memory (i.e. mostly heap).
Time ago, I read on some blog about optimizations for OLPC that the funny thing is that those cached bitmaps are _uncompressed_ (yes, even for JPEGs), and that Firefox caches all bitmaps from all pages. That's a real pity, since decompressing them on the fly would be surely faster than swapping, and maybe even faster than loading them from memory (not sure on this, unless the graphic card supports loading JPEGs in memory).
ELC: How much memory are applications really using?
Posted Nov 1, 2009 13:31 UTC (Sun) by markseger (guest, #57103) [Link]
And if you really like ganglia you can pass anything to it collectl can
collect, not via some whimpy shell interfaces that always have too much
overhead, but via sockets for almost not overhead. That's why the folks at
PNNL use collectl on their 2300 node cluster as their ganglia monitoring
agents.
But that's not why I'm posting this but rather to let people know I've
recently released a set of utilities that add graphics to collectl. You
can
either generate plots from previously gathered data OR do it in real-time.
To read more, check out http://collectl-utils.sourceforge.net
-mark
Copyright © 2007, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds