27

Is there any way to tell whether a particular application running on Mac OSX (10.2+) has some of its memory swapped out (i.e., to one of the /private/var/vm/swapfile* files)? And how much?

Bounty (150 rps)

Simple question. The first correct answer will be accepted if I see it with less than 2 days of expiration of the bounty period. If I still haven't got one then, I'll award it to whoever tells me the most interesting new fact. And if I didn't learn a single thing from any of the answers, I'll award the bounty to whoever writes most prettily.

2
  • This is such a bad question. The number you are looking for would change because you were trying to measure it (think Heisenberg uncertainty principle). Further it would change all the time depending on other applications and their memory requirements, io patterns and process priority. If you find the number it is not going to do you any good unless you are editing OS code, because there is little that you are going to do from the application's point of view to change it.
    – gavaletz
    Feb 10, 2010 at 2:35
  • 3
    @gavaletz: Get out of the wrong side of bed this morning? I want to the kind of swap profiling that Linux makes possible using /proc/$pid/smaps. I can usually identify sudden bursts of swap activity using my ears, so I am not too bothered by gavaletz's uncertainty principle. Feb 10, 2010 at 6:50

5 Answers 5

23
+150

I've been googling alot ;-) As I understand it, the virtual memory of a given process is divided into pages that are handled by the OS and presented to the application as if it were RAM.

In OS X, based on the Mach kernel, this is handled by a daemon called dynamic_pager. This process generates the swapfile(s) in /private/var/vm as you mention. These swapfiles are not generated on a per application basis, but on a "need memory" basis. The swapfiles are divided into pages of 4096 bytes, and the pages are then allocated to the processes who (are deemed by the OS to) need virtual memory. Hence, you cannot associate a swapfile with a given application, but you can see how many pages a given process is using.

You might want to try the command vm_stat in Terminal. This gives you a statistic of VM usage (note that 'page size' times number of pages active equals the size of your swapfile(s)). This also explains why you can have multiple processes using VM, but only a couple of swapfiles.

Other fun commands are vmmap [process id] and pagestuff.

5
  • By the way, check out developer.apple.com/mac/library/documentation/Darwin/Conceptual/… if you want to know some more.
    – trolle3000
    Feb 6, 2010 at 7:57
  • This looks promising. This seems to provide a definite answer to the first part of the question, and is suggestive of a concrete answer to the second part. I need to dig further... Feb 6, 2010 at 15:32
  • Isn't vmmap [pid] or vmmap -pages [pid] the answer for the second part of your question?
    – trolle3000
    Feb 9, 2010 at 18:26
  • vmmap $pid does give unambiguous information about what it classifies as writeable regions. It does not distinguish between allocated (ie. swappable) and swapped memory for readonly regions, but maybe there the distinction isn't so important. I'll very likely accept this answer tomorrow. Feb 10, 2010 at 6:37
  • @trolle3000, Link down..
    – Pacerier
    Apr 23, 2020 at 20:56
9

Based on the ideas posted here I created this little line of code:

sudo vmmap notifyd | grep -A3 'Summary'

which displays the Summary section (3 lines) of the vmmap output. I've used notifyd in this example, but you can replace that with any PID you know of.

This line will try to list all Summary lines of all running processes. Obviously some will fail because their process id is already gone (process ended), but in general i found this is a great way to scroll through a list of memory information and spot the top swapper.

ps -o pid= -xa | awk '{print $1}' | xargs -n 1 sudo vmmap | grep -A3 'Summary'

Edited: Some anonymous user saw this last command line needed an improvement because obviously the original variant did not work anymore. So thank you very much whoever you are and i'm sorry your edit was rejected. (First command previously read 'ps xa' and resulted in vmmap to fail because of the headline of ps being thrown at it)

Further improvement: If you like to know the name of the program right away use this small change

ps -o pid= -xa | awk '{print $1}' | xargs -n 1 sudo vmmap | egrep 'swapped_out|Path'

A little amendment on the other end of this command enables you to filter for certain program names or command line path components. Here we're looking at all processes from 'Library/PrivateFrameworks' only for example.

ps -o pid,command= -xa | grep 'Library/PrivateFrameworks' | awk '{print $1}' | xargs -n 1 sudo vmmap |
1
  • I get a lot of failures saying vmmap could not be launched as a 32-but process, which halts xargs. To get cards to continue, I did ps -o pid= -xa | awk '{print $1}' | xargs -n 1 -I{} bash -c ‘sudo vmmap {} || true’ | egrep 'swapped_out|^Path:|^Process:'. It still doesn’t stop on ctrl+c, but it also doesn’t stop on vmmap errors. Jan 9, 2020 at 7:07
8

The vmmap PID command should give you some helpful stats in numbers about a given process.

1
  • even vmmap [AppName] works
    – Studer
    Feb 10, 2010 at 16:30
1

As ridogi said, open Activity Monitor. Navigate to the process you want to inspect, and click... Inspect! Then click memory; this will tell you how much swap the given process is using.

Have fun!

1
  • 4
    The inspect page tells me all kinds of interesting things that are positively correlated with what the answer of the question is, like #pagefaults, #pageins, and the list of mmapped files, but nowhere the actual answer to the question I am after. Feb 5, 2010 at 21:12
0

Open up Activity Monitor in your /Applications/Utilities folder and there is a Virtual Memory column that will tell you this. You can click on the Virtual Memory header to sort by most or least used by application. Also be sure to change My Processes to All Processes, and be aware that one application could have multiple processes. For example the 3rd party application Little Snitch is comprised of "Little Snitch Network Monitor" and "Little Snitch UIAgent."

5
  • 5
    That's wrong. The nubmer in virtual memory column is a sum of memory currently in RAM and on swap, mmaped files and I don't know what else. Jan 18, 2010 at 19:09
  • Are you sure? Apple doesn't mention it support.apple.com/kb/TA20517
    – ridogi
    Jan 18, 2010 at 21:40
  • 1
    The VM number measures the amount allocated but not actually used yet. The swap files will have what is actually used.
    – Chealion
    Jan 19, 2010 at 0:43
  • @Chealion: how do you define memory that's allocated but not actually used? Jan 20, 2010 at 8:24
  • @tkadlubo: In this case, my belief is that it's just the discrepancy between the VM size stated available for a process versus the actual pages, paged out to disk. I'm not sure how to define it beyond noting that the VM number and the swap files on disk are different.
    – Chealion
    Jan 20, 2010 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.