How A Lot Memory Is My Program Actually Using?
How Much Memory Is My Program Really Using? It appears like such a simple query - however virtual memory makes measuring actual memory use complicated. Shared memory shouldn't be apportioned per process using it so it is overstated. Resident memory includes shared memory, so it overstates as nicely. Digital memory includes resident memory plus information held in swap, which isn't in essential memory. Fortunately until you are working a Linux kernel older than the last financial crisis, you have a greater option: Proportional Set Size (PSS)¹. This is the quantity of non-public memory the process is utilizing, plus its proportional allocation of shared memory. The file lists each memory block mapped to the method, Memory Wave and its properties. You could possibly wrangle the smaps information with shell code, however you don’t need to! The smem instrument does that, and it can filter/format too. Memory concepts can be a bit abstract, so let’s take a look at an instance.
Now what do you suppose the output will present? The youngster course of receives a copy of its parent’s memory - so has actual memory use doubled? No! The guardian and child share the memory; if the little one tries to put in writing to any memory it inherited, the kernel will copy the memory web page for the youngster to put in writing to (this is called copy-on-write). The overall resident memory is 167,280 KB however we know that’s a lie - there’s one large array in memory and both processes are sharing it. The entire PSS of 82,839 KB is extra correct. If my brainwave audio program exits, how a lot memory will be reclaimed? Whilst Proportional Set Dimension is useful for gauging how a lot memory a process is utilizing, brainwave audio program it overstates how a lot memory will probably be freed when the method exits as shared memory can't be reclaimed. To measure free-able memory, use Unique Set Size (USS); the sum of all memory pages personal to the method.
Conveniently, smem already experiences USS so there’s no want to jot down your personal answer until you’re curious³. This Bash one liner prints its own memory statistics by working smem and filtering the output to itself. The USS is 260 KB so I expect to free that much real memory when it exits. At 341 KB, PSS is 81 KB greater, and comprised of its proportional share of /bin/bash (already in memory as I’m launching this from a Bash shell), libc and other shared libraries, and the system locales. The Bash program begins quicker as by sharing the libraries already in memory, the kernel doesn’t have to copy them to the program’s memory. Moreover, shared memory reduces total memory use. Virtual memory complexifies operating programs but it has quite a bit of benefits. 1. PSS was created by Matt Mackall - this LWN article has extra information on its origins. Android taking too lengthy to sample giant processes’ memory to be able to stability memory swimming pools. 3. And if you are curious, this neat golang script by Viacheslav Biriukov calculates the Distinctive Set Measurement for a given PID. The pagemap interface and USS algorithm is described here.