Exercise
Enhance your skills and understanding with the help of the questions provided in this exercise!
We'll cover the following
In this exercise, 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!).
#include<stdio.h> int main() { printf("hello, world\n"); return 0; }
Questions
-
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? -
Next, in the editor above, create a little program that uses a certain amount of memory using the file 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.
You have to press the Run button to register your changes from the editor to the terminal. Then, you can compile and run the program using the following command:
gcc memory-user.c -o memory-user && ./memory-user
-
Now, while running your
memory-user
program, also (in a different terminal window, but on the same machine) run the free 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? -
Let’s try one more tool, known as
pmap
. Spend sometime and read thepmap
manual page in detail. -
To use
pmap
, you have to know the processID 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? -
Finally, let’s run
pmap
on yourmemory-user
program, with different amounts of used memory. What do you see here? Does the output frompmap
match your expectations?
Get hands-on with 1400+ tech skills courses.