Virtual memory (swap) in linux

 

How much memory does your machine have:

free -om

What about the split-- how much of it is in swap rather than in regular memory? Obtain the memory exhauster program, memory3.c, per your instructor. Then compile it:

gcc  memory3.c  -o  memory3

Run the memory exhauster and see where it stops:

./memory3

Write down both how much memory was available to the machine at the time, and where the program stopped.

The place that houses your swap space is very likely a disk partition. Identify it and note its name:

fdisk  -l

Supposing that it's /dev/sda2 (might be different on your box), turn it off:

swapoff  /dev/sda2

Recheck available memory and note disappearance of swap:

free -om

Run the memory exhauster, note where it stops, and compare with where it stopped:

./memory3

Write down both how much memory was available to the machine at the time, and where the program stopped.

A file could also house swap space. Make one whose size is about the same as that of the now-disabled swap partition. For example if it's a 2048MB (i.e., 2GB) partition, then make a 2048MB file. Notice what number of megabytes "free -om" showed for the swap partition (the "m" means show megabyte units) and use that number as dd's "count=" argument here:

dd  if=/dev/zero  of=~/myswapfile  bs=1048576  count=2048

You could adjust the "count" parameter to the desired number of megabytes for a different size if you wanted. The file will be full of zeros. Start using it for virtual memory swapping:

swapon  ~/myswapfile

You'll get an error message, because it needs a little bit of internal formatting and currently it's nothing but zeros. Check that a couple of different ways. First:

od  -Ad  -tx1z  ~/myswapfile

This shows a hex dump for the whole file, the value of each individual byte. They are all zeroes. Second:

strings  ~/myswapfile

This shows all occurences of 4 or more consecutive printable ASCII characters. zero is not a printable ASCII character so it doesn't print anything. When this file gets used for swap purposes the contents of memory will get-- guess what?-- swapped into it. If we run strings on it after that there should be some output.

Now format the file internally as swap space:

mkswap  ~/myswapfile

Check the formatting again, it'll be slightly different (a very modest header written into the very beginning of the file) and readied for the swapping role:

od  -Ad  -tx1z  ~/myswapfile

Turn it on:

swapon  ~/myswapfile

Check available memory:

free  -om

and exhaust it:

./memory3

Note how far it gets. Write down both how much memory was available to the machine at the time, and where the program stopped.

Now, in addition to the swap file turn the swap partition back on too:

swapon  /dev/sda2

Check available memory:

free  -om

and its split between sources:

swapon  -s

and exhaust it:

./memory3

How far did it get? Write down both how much memory was available to the machine at the time, and where the program stopped.

What were the approximate memory consumption limits at which the program was terminated, and what was their relationship to the amount of swap available at the time?

 

We want to clean up by deleting the swap file we made, but first check to see that it now contains a lot of printable (and no doubt also nonprintable) content that wasn't there before:

strings  ~/myswapfile

Anything in there other than zeroes got there by the swapping operation, and reflects stuff that was the contents of memory at some point. Now let's clean up:

rm  ~/myswapfile