In this homework, we’ll just learn about a few useful tools to examine virtual memory usage on Linux-based systems. This will only be a brief hint at what is possible; you’ll have to dive deeper on your own to truly become an expert (as always!).
-
The first Linux tool you should check out is the very simple tool
free
. First, typeman free
and read its entire manual page; it’s short, don’t worry! -
Now, run
free
, perhaps using some of the arguments that might be useful (e.g.,-m
, to display memory totals in megabytes). How much memory is in your system? How much is free? Do these numbers match your intuition?Total: 875M
Free: 587M -
Next, create a little program that uses a certain amount of memory, called
memory-user.c
. This program should take one command-line argument: the number of megabytes of memory it will use. When run, it should allocate an array, and constantly stream through the array, touching each entry. The program should do this indefinitely, or, perhaps, for a certain amount of time also specified at the command line. -
Now, while running your
memory-user
program, also (in a different terminal window, but on the same machine) run thefree
tool. How do the memory usage totals change when your program is running? How about when you kill thememory-user
program? Do the numbers match your expectations? Try this for different amounts of memory usage. What happens when you use really large amounts of memory?[1] 9650 segmentation fault ./memory-user.out 900000 10
-
Let’s try one more tool, known as
pmap
. Spend some time, and read thepmap
manual page in detail. -
To use
pmap
, you have to know the process ID of the process you’re interested in. Thus, first runps auxw
to see a list of all processes; then, pick an interesting one, such as a browser. You can also use yourmemory-user
program in this case (indeed, you can even have that program callgetpid()
and print out its PID for your convenience). -
Now run
pmap
on some of these processes, using various flags (like-X
) to reveal many details about the process. What do you see? How many different entities make up a modern address space, as opposed to our simple conception of code/stack/heap?The memory map of a process.
C libraries.
-
Finally,let’s run pmap on your
memory-user
program, with different amounts of used memory. What do you see here? Does the output from pmap match your expectations?